0

在运行下面的代码时,条件 (1) 和 (3) 不会在 Matlab 中读取。我尽了最大的努力,但无法弄清楚错误。任何帮助都感激不尽。

 % inputs are a_s, p, t, a
 % a_s=single number
 % p,t,a are column vectors
 % output is P (also a column vector)

 if a_s<a<=a_s-180
     if p<=180-t    %------(1)
         P=p+t;
     elseif p>180-t %------(2)
         P=p+t-180;
     end
 elseif a<=a_s | a_s-180<a
     if p>=t        %------(3)
         P=p-t;
     elseif p<t     %------(4)
         P=p-t+180;
     end
 end
4

1 回答 1

1

尝试以下替换:

替换这个:

 if p<=180-t    %------(1)
     P=p+t;
 elseif p>180-t %------(2)
     P=p+t-180;
 end

为了这:

P = p+t;
P(P<=180) = P(P<=180)-180;

和这个:

 if p>=t        %------(3)
     P=p-t;
 elseif p<t     %------(4)
     P=p-t+180;
 end

为了这:

P = p-t;
P(P<0) = P(P<0)+180;

至于两个ifs,a_s不清楚a是在any()条件为真时执行分支还是仅在所有条件都为真(这是默认设置)时执行分支。请记住,这a是一个向量,a<a_s布尔向量也是如此。

于 2012-12-16T03:15:01.133 回答