【问题标题】:Odd 'while' loop behavior with a subfunction call (MATLAB)带有子函数调用的奇怪“while”循环行为(MATLAB)
【发布时间】:2012-10-27 01:52:40
【问题描述】:

所以我有一个我无法弄清楚的奇怪问题。 我有一个 while 循环,它根据索引将值传递到向量中。这些值由调用的子函数提供。我的问题是,在第一次迭代之后,向量停止接受来自子函数的值,尽管代码继续运行并且没有崩溃。 这发生在“S(i)=Trials;”。

function Simulation()
N=input('How many trials would you like to run? ');
Tup=input('What is the positive boundary? ');
Tdown=input('What is the negative boundary? ');
StepLength=input('What is the step length? ');
d=0;
i=1;
S=zeros(1,N);
StepLimit=1000000;
if Tup==Tdown
     display('Please use boundaries that are not equal to eachother.');
 elseif Tup<=d||Tdown>=d
         display('Please choose logical boundaries.');
else
    while i<=N
    S(i)=Trials;
    i=i+1;
    end
end
x=0:10:max(S);
hist(S,x);
axis tight;
xlabel('Number of Steps');
ylabel('Number of Trials');

function s=Trials()
s=0;
    while ~(d<=Tdown)&&~(d>=Tup)
        m=StepLength.*RandDir(1);
        d=d+m;
        s=s+1;
        if s>=StepLimit
            display('The step limit was reached.');
            return
        end
    end

    function out = RandDir(N)
    % Generate a random vector from the set {+/- e_1, +/- e_2,..., +/- e_N}
    % where e_i is the ith basis vector. N should be an integer.
    I = round(ceil(2*N*rand));
        if rem(I,2) == 1
            sgn = -1;
        else
            sgn = 1;
        end
    out = zeros(N,1);
    out(ceil(I/2)) = sgn*1;
    end
end
end

感谢您的帮助

【问题讨论】:

    标签: function matlab error-handling while-loop


    【解决方案1】:

    不涉及语义,这就是你正在做的事情:

    第 1 步:调用函数 Trials... 更改 d 直到获得所需的值。

    第 2 步:再次调用 Trials,但保持与之前使用的 d 相同。它是否满足先前的结束条件?是的。停止试用

    第 3 步:再次调用 Trials,但保持与之前使用的 d 相同。它是否满足先前的结束条件?是的。停止试用

    第 4 步:再次调用 Trials,但保持与之前使用的 d 相同。它是否满足先前的结束条件?是的。停止试用

    解决方案:

    在您的Trials 函数中重新初始化d

    function s=Trials()
        d=0;
        s=0;
    ...
    

    【讨论】:

      【解决方案2】:

      我同意重新初始化您的 s 和 d 变量。您发现难以调试的原因是您正在使用全局变量。除非万不得已,否则请不要这样做!

      这是一个没有全局变量的版本:

      function foo()
      N           = 10;
      Tup         = 10;
      Tdown       = -10;
      StepLength  = 1;
      d           = 0;
      S           = zeros(1,N);
      StepLimit   = 1000000;
      
      if Tup==Tdown
          display('Please use boundaries that are not equal to eachother.');
      elseif Tup<=d||Tdown>=d
          display('Please choose logical boundaries.');
      else
          for i_trial= 1:N
              S(i_trial) = Trials(Tdown, Tup, StepLength, StepLimit);
          end
      end
      
      x           = 0:10:max(S);
      hist(S,x);
      axis tight;
      xlabel('Number of Steps');
      ylabel('Number of Trials');
      
      function s=Trials(Tdown, Tup, StepLength, StepLimit)
      s = 0;
      d = 0;
      while ~(d<=Tdown)&&~(d>=Tup)
          m=StepLength.*RandDir(1);
          d=d+m;
          s=s+1;
          if s>=StepLimit
              display('The step limit was reached.');
              return
          end
      end
      
      function out = RandDir(N)
      % Generate a random vector from the set {+/- e_1, +/- e_2,..., +/- e_N}
      % where e_i is the ith basis vector. N should be an integer.
      I = round(ceil(2*N*rand));
      if rem(I,2) == 1
          sgn = -1;
      else
          sgn = 1;
      end
      out = zeros(N,1);
      out(ceil(I/2)) = sgn*1;
      

      你为什么在 main 函数中使用 while 循环(我是否用 for 循环破坏了你的代码)?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-15
        • 2013-12-07
        相关资源
        最近更新 更多