& operator without if

2 views (last 30 days)
daniel molina
daniel molina on 21 May 2020
Answered: Steven Lord on 22 May 2020
Why this rutine:
[0 0]<[1 1] & [0 0]<[-1 1]
return:
[0 1]
how work conditional operator & without if?
  3 Comments
daniel molina
daniel molina on 21 May 2020
yes, I want to know how work AND in this case, that result this answer
>>[0 0]<[1 1] & [0 0]<[-1 1]
ans =
0 1
James Tursa
James Tursa on 21 May 2020
& is the an element-wise operator.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 22 May 2020
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

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!