【问题标题】:Is there any alternative for goto-statements in MATLAB?MATLAB 中的 goto 语句有什么替代方法吗?
【发布时间】:2016-07-12 06:52:50
【问题描述】:

我在 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 语句有其他替代方法吗?

【问题讨论】:

标签: matlab


【解决方案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

【讨论】:

  • 非常感谢,是的。类似这样的东西。我会试试的
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    你可以试试switch-case方法。请看下面的例子;

    caseno = input('input your case no');
    
    switch (caseno)
         case 1
              disp('this first section will run');
         case 2
              disp('this second section will run');
         otherwise
              disp('wrong case no');
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 2021-08-20
      • 2017-03-02
      • 2011-10-01
      相关资源
      最近更新 更多