Summing the values inside a while loop

15 ビュー (過去 30 日間)
Arie
Arie 2023 年 3 月 10 日
回答済み: Voss 2023 年 3 月 10 日
I was wondering how we can sum up the values we get inside a while loop outside of the while loop so for example in a while loop you are getting different scores and by the end of the loop, you want to sum all those scores you get. I used the sum function but it just prints out the last score not all of them!

回答 (1 件)

Voss
Voss 2023 年 3 月 10 日
scores = [];
ii = 1;
while ii <= 10
scores(end+1) = rand();
ii = ii+1;
end
scores
scores = 1×10
0.5831 0.2789 0.4391 0.0140 0.5141 0.7504 0.0295 0.0610 0.1610 0.2630
sum(scores)
ans = 3.0941

カテゴリ

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