Matlab:如何动态更新for循环的限制?
Matlab: How to update the limit of a for loop dynamically?
我正在 matlab 中编写以下代码:
m=unique(x);
for i=1:length(m)
%some code that increase the number of unique values in x
.......
.......
%here I tried to update m
m=unique(x);
end
虽然我已经更新了m
,在for结束前写了这一行m=unique(x);
,但是for循环的极限仍然有相同的旧值。我需要动态更新 for 循环的限制。那可能吗?如果可以,怎么做?
当MATLAB遇到for i = 1:length(m)
时,它将语句转换为for i = [1 2 3 ... length(m)]
。您可以将其视为硬编码。因此,在 for 循环内更新 for-limit 没有效果。
m = unique(x);
i = 1;
while true
if i > length(m)
break
end
% do something
i = i + 1;
m = unique(x);
end
我正在 matlab 中编写以下代码:
m=unique(x);
for i=1:length(m)
%some code that increase the number of unique values in x
.......
.......
%here I tried to update m
m=unique(x);
end
虽然我已经更新了m
,在for结束前写了这一行m=unique(x);
,但是for循环的极限仍然有相同的旧值。我需要动态更新 for 循环的限制。那可能吗?如果可以,怎么做?
当MATLAB遇到for i = 1:length(m)
时,它将语句转换为for i = [1 2 3 ... length(m)]
。您可以将其视为硬编码。因此,在 for 循环内更新 for-limit 没有效果。
m = unique(x);
i = 1;
while true
if i > length(m)
break
end
% do something
i = i + 1;
m = unique(x);
end