How to determine maximum of n times FOR loop iteration

2 ビュー (過去 30 日間)
balandong
balandong 2017 年 8 月 2 日
コメント済み: Star Strider 2017 年 8 月 2 日
Dear all, This may sound so basic, but I had google using various keyword (or maybe I used wrong keyword), but I failed to find good info on how to calculate how many cycle the FOR loop iterate.
For example
% the following FOR loop will iterate max 3 times
for i =0.1:0.3:0.9
a =i
end
AND
% the following FOR loop will iterate max 4 times
for i =0.1:0.3:1
a =i
end
In the above case, I manually check the the first and second FOR loop will cycle 3 & 4 time, respectively. My question is, is there way I can calculate it automatically?
Thanks in advance

採用された回答

Star Strider
Star Strider 2017 年 8 月 2 日
The only way to know in advance the number of iterations a loop can take is to specify them as a separate variable and see the number of elements in it:
v1 = 0.1:0.3:0.9;
iter1 = numel(v1)
for i = v1
a =i
end
iter1 =
3
a =
0.1
a =
0.4
a =
0.7
  2 件のコメント
balandong
balandong 2017 年 8 月 2 日
Thanks so much SS, Really appreciate it!
Star Strider
Star Strider 2017 年 8 月 2 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

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