Reputation: 213
How can I repeat a step in a loop in MatLab?
For example, if a value given for n (loop index) doesn't fit my expectation, I would like to repeat the step again with n, but having changed something.
I tried:
putting a while outside the step of the for, something like
for n=1:N-1
while chkstep == 1 do
(....)
end
end
at the end of the time step decreasing n:
for n=1:N-1
(....)
n=n-1;
end
Upvotes: 0
Views: 850
Reputation: 181
How about this
for n = 1:N-1
check = true;
notChanged = true;
while check
(..Do your thing..)
if (n ~= goodValue && notChanged)
(...make the change...)
notChanged = false;
continue;
end
break;
end
end
Upvotes: 0
Reputation: 11802
I am not sure of what is wrong with your first solution, it should work as you describe. But you could also use a while
for the outer loop, and only increment the counter n
when you are satisfied with the current step.
Something like:
n = 1
while (n<N)
while chkstep == 1 do
(....)
end
n = n+1 ;
end
note about your second solution: Matlab for
loop are static, changing n
from within the loop will not change the number of time the loop will run.
Upvotes: 1