usumdelphini
usumdelphini

Reputation: 213

Repeating a step in a loop

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:

Upvotes: 0

Views: 850

Answers (2)

akhilc
akhilc

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

Hoki
Hoki

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

Related Questions