2017-06-10 26 views
1

MATLABのこのループでは、 'if - end'の後に、次の実行を行わずに同じループに戻りたいとします。i。具体的には、check(i)0と異なるまで、MATLABに確認するように伝えたいと思います。ループ内のMatlabループ

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

答えて

2

それが決定されると、あなたはforループの繰り返し回数を変更することはできません。このような場合にはループをwhileとしてください。

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 
0

使用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