How to use a for loop to calculate the sum of a row vector?
211 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Stalin Samuel
2015-11-25
编辑:Stephen23
2018-12-21
x = [1, 23, 43, 72, 87, 56, 98, 33] ;
y = 0;
for n = 1:length(x)
y = y + x(n);
end
8 个评论
更多回答(1 个)
Aza Xongo
2019-4-16
编辑:Aza Xongo
2019-4-16
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)
1 个评论
Yadu Bhusal
2021-8-14
X =['enter an array>']; m = length(x); Sum = 0; For i = 1:m Sum = sum + X(i); end Disp(sum)
IF you want single sum. It you wish iterations remove semicolon from end of X(i).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!