Loop order/ assignement
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Walter Roberson
on 15 Jan 2016
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 Comments
Walter Roberson
on 15 Jan 2016
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.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!