MATLAB: & operator without if

conditional

Why this rutine:
[0 0]<[1 1] & [0 0]<[-1 1]
return:
[0 1]
how work conditional operator & without if?

Best Answer

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.
a = [true false true];
b = [true true false];
c = a & b; % c(1) = a(1) & b(1), c(2) = a(2) & b(2), c(3) = a(3) & b(3)
The & operator with two scalars as input returns true if both its inputs are non-zero and false otherwise.
[ true & true; ... % true
true & false; ... % false
false & true; ... % false
false & false] % false