Problem 3. Find the sum of all the numbers of the input vector
Find the sum of all the numbers of the input vector x.
Examples:
Input x = [1 2 3 5] Output y is 11
Input x = [42 1] Output y is 41
Solution Stats
Problem Comments

56 Comments
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);
end '
smaller?
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).
Easy
thank you
nice
yay
nice
very easy
yes
can i get the answer?
Easy to solve. But how to reach better result???
y=sum(x)
good
Great Problem!
good usage of ans
Nice one.
great problem
vecsum doesn't work in my 2018b software and I can't find vecsum in my documentation. Where is it?
y=sum(A)
y=sum(x) is not working
Easy!
It's easy. A good problem to start for beginners!
good
good question
this was fun and challenging
simple and sweet
good practice
Nice!!
nice practice
easy
nice
good practice
Very easy
not too difficult
I was originally confused by the assertion code but I understand now.
not bad
easy problem
pretty easy
piece of cake!
too ez
really beginner friendly problem!
gg ez
nice problem
Nice problem
Really nice problem!
nicely structured problem!
Good Problem!
This is a very nice problem!
This was a fun little problem
Solution Comments
Show commentsProblem Recent Solvers46093
Suggested Problems

Find the numeric mean of the prime numbers in a matrix.
8653 Solvers

Increment a number, given its digits
636 Solvers

Project Euler: Problem 4, Palindromic numbers
961 Solvers

1382 Solvers

481 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!