The & operator is an elementwise operator. Just like the + operator will add corresponding elements of its inputs when given two vectors of the same size:
x = [1 2 3];
y = [4 5 6];
z = x + y % z(1) = x(1) + y(1), z(2) = x(2) + y(2), z(3) = x(3) + y(3)
The & operator will take the and of the corresponding elements of its inputs.
The basic problem is that you are attemptingtwological relations simultaneously: MATLAB's relational operators are binary operators, so they only support one comparison at once. In summary:
X<A<Y % is NOT valid for comparing A against X and Y
X<A & A<Y % This compares A with both X and Y
In MATLAB logical relational operators follow the standard rules ofoperator precedence. Look at this example carefully:
>> 3<4<2
ans =
1
The output is true, so does this mean that4<2 ? Of course not, we just have to remember theoperator precedence rules, just like in high school. This is evaluated according to those rules as:
>> (3<4)<2
ans =
1
where
>> 3<4
ans =
1
So3<4<2 is actually equivalent to1<2, which is of course very true:
Best Answer