Explain this while loop

2 ビュー (過去 30 日間)
Dave Smith
Dave Smith 2013 年 2 月 26 日
Please explain the steps to how this code fragment works. I don't really understand count or index. Thanks.
What is count as a result of the given Matlab code fragment? Showing your work
may result in partial credit.
numbers = [2 4 3 -5 1 0];
count = 0;
index = 1;
while numbers(index) > 0
count = count + 1;
index = index +1;
end
count = 3
  1 件のコメント
Walter Roberson
Walter Roberson 2013 年 2 月 26 日
"count" and "index" are just variable names. You could have used XYZ and Chocolate_IceCream instead and MATLAB would not have cared.

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 26 日
編集済み: Azzi Abdelmalek 2013 年 2 月 26 日
numbers(1)=2
numbers(2)=4
numbers(3)=3
numbers(4)=-5 % before numbers becomes <0 the counter=3

その他の回答 (1 件)

Youssef  Khmou
Youssef Khmou 2013 年 2 月 26 日
編集済み: Youssef Khmou 2013 年 2 月 26 日
hi Dave :
Literally " Given a vector of length N, count the number of positive elements before the first negative one, as long as the numbers are positive count them but when a <0 number is met, stop the counting"

カテゴリ

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