Loop order/ assignement
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
I am writing a loop say : for i=1:9;
Suppose I am in the 5th iteration
if certain conditions of my If else statement are met, then I want my next loop to start from iteration nbr5 not from i=6.
82 4.405517686 6.551066224 8.37093675 9.377805584
70 3.549776369 5.46044706 7.393870673 8.899016011
41 3.925365061 6.109160353 8.117877657 9.186700016
85 3.258680565 5.152732852 7.091023754 8.418727381
99 4.047440897 6.097040296 7.774985958 8.495417896 ( 5th row: i=5).
so in i+1= 6, I want my loop to actually still start from i=5 ( 5th row),
is this even possible in Matlab?
can someone PLEASE help??
0 件のコメント
回答 (1 件)
Walter Roberson
2016 年 1 月 15 日
No. You will need to switch from using a "for" loop to using a "while" loop.
(There are also some ways of writing it that involve putting a while loop within the for loop.)
2 件のコメント
Walter Roberson
2016 年 1 月 15 日
Two forms:
i = 1;
while i <= 9
... do something ...
if conditions are *not* met
i = i + 1;
end
end
or
for i = 1 : 9
loop_again = false;
while loop_again
... do something ...
if conditions are met
loop_again = true;
end
end
end
The first of those obviously takes less code. It also has the possibility of going backwards by subtracting from i.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!