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

227 ビュー (過去 30 日間)
Michelle S
Michelle S 2015 年 11 月 25 日
コメント済み: Yadu Bhusal 2021 年 8 月 14 日
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?

採用された回答

Stalin Samuel
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 件のコメント
Christopher Vargas
Christopher Vargas 2020 年 4 月 6 日
disp(y) %this will display the result
Lexi Finke
Lexi Finke 2020 年 10 月 5 日
how do you make this loop into a function??

サインインしてコメントする。

その他の回答 (1 件)

Aza Xongo
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
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 ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by