Loop order/ assignement

2 views (last 30 days)
Amine Ben Ayara
Amine Ben Ayara on 15 Jan 2016
Commented: Walter Roberson on 15 Jan 2016
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??

Answers (1)

Walter Roberson
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
Amine Ben Ayara
Amine Ben Ayara on 15 Jan 2016
Hello Walter, Thank you for your reply. Just just to clarify, If I write for loop: for i=1:6 or 7, the while loop goes it right below it? for i=1:6
Walter Roberson
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.

Sign in to comment.

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!