【问题标题】:Matlab loops in loopMatlab循环循环
【发布时间】:2017-11-12 13:18:42
【问题描述】:
对于 MATLAB 中的这个循环,在“if - end”之后,我想返回同一个循环而不执行下一个i。更具体地说,我想告诉 MATLAB 进行检查,直到 check(i) 与 0 不同。
for i = 1:length(numDate)
check(i)=any(Dates == numDate(i));
if check(i) == 0
numDate(i) = numDate(i)-1;
end
end
【问题讨论】:
标签:
matlab
loops
for-loop
if-statement
while-loop
【解决方案1】:
使用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
【解决方案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