BlueFx
BlueFx

Reputation: 67

Matlab loops in loop

For this loop in MATLAB, after the 'if - end' I want to return to the same loop without executing next i. More specifically, I want to tell MATLAB to check until check(i) is different from 0.

for i = 1:length(numDate)
    check(i)=any(Dates == numDate(i));
    if check(i) == 0
        numDate(i) = numDate(i)-1;
    end
end 

Upvotes: 1

Views: 89

Answers (2)

Sardar Usama
Sardar Usama

Reputation: 19689

You cannot change the number of iterations of a for loop once it is decided. Use while loop for such a case.

k=1; %I replaced the loop variable with k because i (and j) are reserved for imag no.s
while k<=length(numDate)
   if any(Dates == numDate(k)) == 0
        numDate(k) = numDate(k)-1;
   else k=k+1;  %increment only if the condition is not satisfied
   end
end

Upvotes: 2

Wadaane
Wadaane

Reputation: 326

Use break

for i = 1:length(numDate)
  check(i)=any(Dates == numDate(i));
    if check(i) == 0
       numDate(i) = numDate(i)-1;
    else  
       break
    end
end

Upvotes: 0

Related Questions