在MATLAB中有goto语句的替代方法吗?

时间:2016-07-12 06:52:51

标签: matlab

我在MATLAB中编写程序时遇到问题。我需要一个goto语句来解决我的问题。这是代码:

for i=1:n
1:  newT=T(i)+unifrnd(-r,r);
    newT=P(i)+unifrnd(-r,r);
    if newT<Tmax && newT>Tmin && newP<Pmax && newP>Pmax
        bestT=newT bestP=newP
    else
        go to 1
    end
end 

那个goto语句还有其他选择吗?

3 个答案:

答案 0 :(得分:1)

这就是你想要的东西吗?

for i=1:n
    loop = true;
    while loop
        newT=T(i)+unifrnd(-r,r);
        newT=P(i)+unifrnd(-r,r);
        if newT<Tmax && newT>Tmin && newP<Pmax && newP>Pmax
            bestT=newT bestP=newP
            loop = false;
        end
    end 
end

答案 1 :(得分:0)

您可以在MathWorks®上使用此提交:https://www.mathworks.com/matlabcentral/fileexchange/26949-matlab-goto-statement

但是,我建议您修改代码而不是使用它!

答案 2 :(得分:0)

您可以尝试切换大小写方法。请参见下面的示例;

salut()
相关问题