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

Show
15 older comments

James
on 21 Feb 2012

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.

Ali
on 8 Mar 2012

James, a vector traditionally denotes a 1xd or dx1 matrix (with d > 1). If d = 1, we tend to call that a scalar.

Rohan Kale
on 16 Jun 2012

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);

VIVEK RUHELA
on 12 Nov 2014

What is the size of vector....is it [1*n] or [n*m]

AM
on 28 Nov 2015

Well said, Rohan.

Gage Noyes
on 23 Dec 2015

I'm only commenting for the badge

Ebony
on 27 Jan 2016

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?

Folds the Paper Man
on 8 Mar 2016

solved it on first try

ZeHua Xiao
on 18 Sep 2016

You don't need write x=[1 2 3 5]. He ask us write a function. So, it should be y=sum(x).

Michael Bohorquez
on 17 Dec 2016

Easy

Gan Wee Ting
on 20 Dec 2016

thank you

sim yoke meng
on 20 Dec 2016

nice

Adi Zabidi
on 27 Dec 2016

yay

Kunwar Abdul Rahman
on 27 Dec 2016

nice

Abubaker SHERIF
on 29 Dec 2016

very easy

MEI HUEI LIM
on 30 Dec 2016

yes

SIVAPRAKASH DANABALASINGAM
on 10 Jan 2017

can i get the answer?

Stephan Jung
on 8 Jun 2017

Easy to solve. But how to reach better result???

1 Comment

irem erezn
on 17 Jan 2017

Why doesn't it work?
function y=vecsum(x)
y=symsum(x(i),i,1,size(x));
end

1 Comment

SIVAPRAKASH DANABALASINGAM
on 10 Jan 2017

very easy

1 Comment

Devan Rao
on 10 Jan 2017

nice

1 player likes this solution

1 Comment

TAYALAN SIVALINGAM
on 29 Dec 2016

good job

1 player likes this solution

1 Comment

Haitong Ye
on 23 Oct 2016

Nice!

1 player likes this solution

1 Comment

SUBROTA HALDER
on 1 Sep 2016

easy :)

1 player likes this solution

2 Comments

Md.Farhad Hassan
on 25 Jul 2016

can anyone please help me to find out the where is the mistake, i am a new learner .........

Atsushi Ueno
on 25 Jul 2016

You have to define a function vecsum() like:
function y = vecsum(x)
y=0;
for x= 0:1:10
y=x+y;
end
end
because your answer is executed by calling function in test suite below. The function's name is decided by problem creater.

1 player likes this solution

1 Comment

Pim van der Sluis
on 23 May 2016

This solution works.

2 players like this solution

1 Comment

lis coffey
on 14 Jul 2016

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?
Thanks
http://www.fixithere.net/sky-customer-service/

2 players like this solution

1 Comment

Sierra McGee
on 13 Nov 2015

Yay! I got it right! Omg I'm so happy!

1 player likes this solution

1 Comment

Sierra McGee
on 13 Nov 2015

What does this solution mean. In other words, can someone explain the solution step-by-step.

2 players like this solution

4 Comments

Show
1 older comment

VIVEK RUHELA
on 14 Nov 2014

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......

Tim
on 14 Nov 2014

The "y=" is missing from "y=sum(x)".

VIVEK RUHELA
on 15 Nov 2014

@Mr. Tim . Atleast You give me positive reply.Thanks a lot.Can I further talk to you regarding MATLAB CODY Guidence..

Tim
on 19 Nov 2014

Sure, ok.

1 player likes this solution

1 Comment

Carlos Pavon
on 16 Jun 2014

Bien Javier

1 player likes this solution

3 Comments

fert
on 22 Feb 2014

y = sum(x)
x=[1 2 3 5]
is not that true, i am confused.

Thomas Blackwood
on 5 Sep 2016

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 ;
;)

Thomas Blackwood
on 5 Sep 2016

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 ;

1 player likes this solution

1 Comment

Andrew Newell
on 7 Jun 2013

This solution is just for a laugh - the test suite is pretty weak!

2 players like this solution

3 Comments

8 players like this solution

3 Comments

HaveF
on 17 Dec 2012

although ans trick can make the code short... but I don't like it...

Lajos
on 10 May 2014

I agree with HaveF.

Barry Becker
on 21 Nov 2015

Not to mention that it doesn't even run

1 player likes this solution

1 Comment

greatordinary
on 3 Mar 2012

there is definitely something wrong with cody.I am getting 'Undefined function 'cody.verifyCode' for input arguments of type 'char'.' for my solution

3 players like this solution

2 Comments

TAB
on 23 Jun 2012

Test for vactor
x = [1 4;3 4]
It will add columns only.

Ozan Gunaydin
on 22 Sep 2014

Problem is finding the sum of all numbers in an input vector, so the test set is correct.

**Tags**

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
41 players like this problem

41 players like this problem