MATLAB Answers

How to use a for loop to calculate the sum of a row vector?

1,388 views (last 30 days)
Michelle S
Michelle S on 25 Nov 2015
Commented: Christopher Vargas on 6 Apr 2020 at 20:43
I have the values x-[1, 23, 43, 72, 87, 56, 98, 33] How do you calculate the sum (which should equal 413) using a for loop?

  0 Comments

Sign in to comment.

Accepted Answer

Stalin Samuel
Stalin Samuel on 25 Nov 2015
Edited: Stephen Cobeldick on 21 Dec 2018
x = [1, 23, 43, 72, 87, 56, 98, 33] ;
y = 0;
for n = 1:length(x)
y = y + x(n);
end

  7 Comments

Show 4 older comments
Yue ZHAN
Yue ZHAN on 17 Mar 2020 at 0:20
what about multiply these elements together?

Sign in to comment.

More Answers (1)

Aza Xongo
Aza Xongo on 16 Apr 2019
Edited: Aza Xongo on 16 Apr 2019
Write a program (using loops) that calculates and displays the sum of all the elements of x=[2 -5 6 7; -4 8 -5 6];(its a question)

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags