user1907072
user1907072

Reputation:

multiple if conditions in matlab

while running the code below, the conditions (1) and (3) are not read in Matlab. I tried my best but couldn't figure the mistake. Any help will be much appreciated.

 % 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

Upvotes: 0

Views: 863

Answers (1)

R. Schifini
R. Schifini

Reputation: 9313

Try the following substitutions:

Substitute this:

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

for this:

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

and this:

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

for this:

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

as for the two ifs for a_s and a it's not clear if you want to execute the branch when any() condition is true or only if all of them are true (which is the default). Remember that a is a vector, so a<a_s is a boolean vector.

Upvotes: 1

Related Questions