MATLAB Functions | Help Desk |
Logical Operators & | ~
Logical operationsA & 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.
|
---|
not
has the highest precedence.
and
and or
have equal precedence, and are evaluated from left to right.
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