Reputation: 259
I am currently working on a piece of code and the if, elseif, else
statements will not cooperate with me. It is very simple code, most are just constants I've defined:
K1 = 0.174532925;
K2 = 0.20943951;
y0 = [0.0 15.2161 0.0 0.0];
ieout = [];
if isempty(ieout)
if y0(1)>K1
ieout = [ieout 5];
elseif K1>y0(1)>-K1 && y0(2)<0
ieout = [ieout 1];
elseif -K1>y0(1)>-K2 && y0(2)<0
ieout = [ieout 2];
elseif -K1>y0(1)>-K2 && y0(2)>0
ieout = [ieout 3];
elseif K1>y0(1)>-K1 && y0(2)>0
ieout = [ieout 4];
end
end
Now, my vector y0
have all zeroz
except at the second position which is positive, so this means the last elseif
statement should be executed. This is not the case, instead the statement before the last is executed and my vector ieout
gets a scalar element 3
instead of 4
.
This confuses me, why is this happening?
Upvotes: 0
Views: 50
Reputation: 4382
You can't chain logical operators like 1<y<2
. You have to use 1<y && y<2
So:
K1 = 0.174532925;
K2 = 0.20943951;
y0 = [0.0 15.2161 0.0 0.0];
ieout = [];
if isempty(ieout)
if y0(1)>K1
ieout = [ieout 5];
elseif K1>y0(1) && y0(1)>-K1 && y0(2)<0
ieout = [ieout 1];
elseif -K1>y0(1) && y0(1)>-K2 && y0(2)<0
ieout = [ieout 2];
elseif -K1>y0(1) && y0(1)>-K2 && y0(2)>0
ieout = [ieout 3];
elseif K1>y0(1) && y0(1)>-K1 && y0(2)>0
ieout = [ieout 4];
end
end
Upvotes: 2