Matlab for循环中的逻辑与
我想在Matlab for循环中的逻辑与,matlab,loops,for-loop,conditional-statements,Matlab,Loops,For Loop,Conditional Statements,我想在for循环中添加一个附加条件 for(i=1; (i<100)&&(something>0.001) ; i++) { //do something } 逻辑条件用if语句表示 for (y = 1:pI_present_y) if (max_sim_value > threshold) % do something end end 如果max\u simu\u value和threshold中的一个是长度向量pI
for
循环中添加一个附加条件
for(i=1; (i<100)&&(something>0.001) ; i++)
{
//do something
}
逻辑条件用
if
语句表示
for (y = 1:pI_present_y)
if (max_sim_value > threshold)
% do something
end
end
如果max\u simu\u value
和threshold
中的一个是长度向量pI\u present\u y
,则在If
语句中用y
对其进行索引,即循环中的max\u simu\u value(y)
或threshold(y)
,循环变量在这些迭代中的迭代次数和值将在第一次执行时立即选择。由于您希望在每次迭代中检查条件,因此如果不在循环中引入条件,就不能使用
for
循环。这就是我所说的
但是,如果条件不满足,最好使用。这样,它将成为C代码的真实副本。否则循环将继续执行,直到y
等于pI\u present\u y
。结果将是相同的,但将有不必要的迭代,并且循环变量的值在循环结束时将不同。i、 e
for y = 1:pI_present_y-1 %Subtracting 1 because you have i<100 in the C code, not i<=100
if max_sim_value <= threshold
break;
end
%do something
end
for y = 1:pI_present_y-1 %Subtracting 1 because you have i<100 in the C code, not i<=100
if max_sim_value <= threshold
break;
end
%do something
end
y=1;
while(y<pI_present_y && max_sim_value>threshold)
% do something
y=y+1;
end