If x is 3x3 (or 3x3x3) or similarly shaped vector, sum(x) doesn't automatically add up all of the numbers in the entire vector. You need a loop to go through each dimension of x to get the sum of every element.
James, a vector traditionally denotes a 1xd or dx1 matrix (with d > 1). If d = 1, we tend to call that a scalar.
Firstly you'd need to vectorize the elements as in suppose x is 3x3 or any size mxp, let y=x(:); now you can find the sum by employing the function sum,i.e,sum(y);
What is the size of vector....is it [1*n] or [n*m]
Well said, Rohan.
I'm only commenting for the badge
Why is the solution larger when I call it a single? Additionally, how can I make my solution:
' function y = vecsum(x)
y = sum(x);
solved it on first try
You don't need write x=[1 2 3 5]. He ask us write a function. So, it should be y=sum(x).
can i get the answer?
Easy to solve. But how to reach better result???
good usage of ans
vecsum doesn't work in my 2018b software and I can't find vecsum in my documentation. Where is it?
y=sum(x) is not working
It's easy. A good problem to start for beginners!
this was fun and challenging
simple and sweet
I like this task
It's easy when you're turning it into a column vector
How can the leading solution size be 7?
one of the simplest task.
Please remove this is a misleading solution.
Hmmm, are you sure this is the leading solution?
who can tell me what is this????/!!
Ha! Small tricks.
Come on, this one is so easy
I am using either for loop or sum command (twice) but it saying error. Any clue?
I used the "sum" function to solve this problem really quickly.
Why doesn't it work?
Because you are doing it wrong!
You should check the basis of 'sum' topic in the help.
Here you go: https://www.mathworks.com/help/matlab/ref/sum.html?searchHighlight=sum%20vector&s_tid=doc_srchtitle
This user appears to have gamed the system, with fraudulent "likes" of this unremarkable solution submitted by 'sock-puppet' accounts. —DIV
can anyone please help me to find out the where is the mistake, i am a new learner .........
You have to define a function vecsum() like:
function y = vecsum(x)
for x= 0:1:10
because your answer is executed by calling function in test suite below. The function's name is decided by problem creater.
What is your code Sir?
If you write your code here, I can say where is the mistake.
This solution works.
unable to find the sum of all numbers in input vector, so the test set is correct. I make my solution: ' function y = vecsum(x) y = sum(x); end ' smaller?
Yay! I got it right! Omg I'm so happy!
What does this solution mean. In other words, can someone explain the solution step-by-step.
why y=sum(x) is wrong. Still I made the same function on MATLAB software and it runs and give correct answer. Can anybody tell me whats going on......
The "y=" is missing from "y=sum(x)".
@Mr. Tim . Atleast You give me positive reply.Thanks a lot.Can I further talk to you regarding MATLAB CODY Guidence..
y = sum(x)
x=[1 2 3 5]
is not that true, i am confused.
You haven't stated the *Function* name, input values and output values. Without them the code from the site cannot check your code.
Secondly you've declared X after you wanted to run a function with it.
The program will read:
Sum of X equals Y. X equals Zero or Not a Number, therefore Y is Zero or Not a Number. Next line, X is [Array]. Okay, X is [Array].
Also good practice to end your functions with a semicolon ;
You haven't stated the *Function* name, input values and output values. Without them the code from the site cannot check your code. Secondly you've declared X after you wanted to run a function with it. The program will read: Sum of X equals Y. X equals Zero or Not a Number, therefore Y is Zero or Not a Number. Next line, X is [Array]. Okay, X is [Array]. Y is still 0 or NaN. Also good practice to end your functions with a semicolon ;
This solution is just for a laugh - the test suite is pretty weak!
although ans trick can make the code short... but I don't like it...
I agree with HaveF.
Not to mention that it doesn't even run
there is definitely something wrong with cody.I am getting 'Undefined function 'cody.verifyCode' for input arguments of type 'char'.' for my solution
Why isn't nansum(x) a solution?
Test for vactor
x = [1 4;3 4]
It will add columns only.
Problem is finding the sum of all numbers in an input vector, so the test set is correct.
Who Has the Most Change?
Energy of a photon
Number of 1s in a binary string
Add two numbers
Narcissistic number ?
Find the longest sequence of 1's in a binary sequence.
Extract leading non-zero digit
Find state names that start with the letter N
Find the treasures in MATLAB Central and discover how the community can help you!
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office