Using a while loop with a vector

79 ビュー (過去 30 日間)
Tim Stark
Tim Stark 2019 年 5 月 2 日
コメント済み: Tim Stark 2019 年 5 月 3 日
Hey guys I'm running in to a problem with a 'while loop'. A simplified version of my codes look likes this:
B = [361;362;363;1000;10000;100000];
while B > 360
B = B - 360;
end
which yields:
B =
1
2
3
640
9640
99640
What I dont understand is this: why does my while loop not repeat until the last three elements of my matrix are < 360.
Thanks in advance

採用された回答

Kevin Phung
Kevin Phung 2019 年 5 月 2 日
Your while loop actually only runs once because after the first iteration, B>360 returns a logical array of [0 0 0 1 1 1], so it does not loop a second time. I think you meant to do something like this:
B = [361;362;363;1000;10000;100000];
for i = 1:numel(B)
while B(i) > 360
B(i) = B(i) - 360;
end
end
which will return:
1
2
3
280
280
280
  1 件のコメント
Tim Stark
Tim Stark 2019 年 5 月 3 日
Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by