Skipping for loop with continue does not work

1 回表示 (過去 30 日間)
Will
Will 2020 年 4 月 7 日
コメント済み: Walter Roberson 2020 年 4 月 7 日
Hi, I'm trying to skip the iteration to the next one if i is equal to 1, but my code does not work:
for v = 0.5:0.05:1.5
if (v == 1)
continue;
end
disp(v);
end
the out is:
0.5000
0.5500
0.6000
0.6500
0.7000
0.7500
0.8000
0.8500
0.9000
0.9500
1.0500
1.1000
1.1500
1.2000
1.2500
1.3000
1.3500
1.4000
1.4500
1.5000
which contains also 1 !!!
How can I fix it ?
  3 件のコメント
Will
Will 2020 年 4 月 7 日
Sorry, you are right. The code works ?
Walter Roberson
Walter Roberson 2020 年 4 月 7 日
However if you had asked to match (say) 1.15 then there might not have been a match, because 0.05 is not exactly representable in MATLAB, and 0.5:0.05:1.5 accumulates round-off error.
v = (50:5:150)/100
would have different round-off error.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 4 月 7 日

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by