gpt4 book ai didi

Matlab:如何动态更新for循环的限制?

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:14 25 4
gpt4 key购买 nike

我正在使用 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:如何动态更新for循环的限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39528474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com