MATLAB Function Reference | Search  Help Desk |
Logical Operators & | ~ | Examples See Also |
A & B A | B ~AThe symbols
&
, |
, and ~
are the logical operators and, or, and not. They work element-wise on arrays, with 0 representing logical false (F
), and anything nonzero representing logical true (T
). The &
operator does a logical and, the|
operator does a logical or, and ~A
complements the elements of A
. The function xor(A,B)
implements the exclusive or operation. Truth tables for these operators and functions follow.Inputs | and |
or |
xor |
not |
|
A |
B |
A&B |
A|B |
xor(A,B) |
~A |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
1 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
0 |
0 |
.not
has the highest precedence.
.and
and or
have equal precedence, and are evaluated from left to right.
and |
|
and(A,B) |
or |
|
or(A,B) |
not |
|
not(A) |
1 & 0 + 3 3 > 4 & 1They evaluate to 1 and 0 respectively, and are equivalent to:
1 & (0 + 3) (3 > 4) & 1Here are two examples that illustrate the precedence of the logical operators to each other:
1 | 0 & 0 = 0 0 & 0 | 1 = 1The relational operators:
<
, <=
, >
, >=
, ==
, ~=
, as well as:
all
Test to determine if all elements are nonzero
any
Test for any nonzeros
find
Find indices and values of nonzero elements
logical
Convert numeric values to logical
xor
Exclusive or