Programming and Data Types | ![]() ![]() |
This section describes the MATLAB logical operators and also covers:
MATLAB provides these logical operators.
Operator |
Description |
& |
AND |
| |
OR |
~ |
NOT |
Note
In addition to these logical operators, the ops directory contains a
number of functions that perform bitwise logical operations. See online help
for more information.
|
Each logical operator has a specific set of rules that determines the result of a logical expression:
AND
operator, &
, is true if both operands are logically true. In numeric terms, the expression is true if both operands are nonzero. This example shows the logical AND
of the elements in the vector u
with the corresponding elements in the vector v.
u = [1 0 2 3 0 5]; v = [5 6 1 0 0 7]; u & v ans = 1 0 1 0 0 1
OR
operator, |
, is true if one operand is logically true, or if both operands are logically true. An OR
expression is false only if both operands are false. In numeric terms, the expression is false only if both operands are zero. This example shows the logical OR
of the elements in the vector u
and with the corresponding elements in the vector v
.u | v ans = 1 1 1 1 0 1
NOT
operator, ~
, negates the operand. This produces a false result if the operand is true, and true if it is false. In numeric terms, any nonzero operand becomes zero, and any zero operand becomes one. This example shows the negation of the elements in the vector u
.~u ans = 0 1 0 0 1 0
Using Logical Operators on Arrays
MATLAB's logical operators compare corresponding elements of arrays with equal dimensions. For vectors and rectangular arrays, both operands must be the same size unless one is a scalar. For the case where one operand is a scalar and the other is not, MATLAB tests the scalar against every element of the other operand. Locations where the specified relation is true receive the value 1
. Locations where the relation is false receive the value 0
.
Logical Functions
In addition to the logical operators, MATLAB provides a number of logical functions
A number of other MATLAB functions perform logical operations. For example, the isnan
function returns 1
for NaN
s; the isinf
function returns 1
for Inf
s. See the ops
directory for a complete listing of logical functions.
Logical Expressions Using the find Function
The find
function determines the indices of array elements that meet a given logical condition. It's useful for creating masks and index matrices. In its most general form, find
returns a single vector of indices. This vector can be used to index into arrays of any size or shape. For example,
A = magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 i = find(A > 8); A(i) = 100 A = 100 2 3 100 5 100 100 8 100 7 6 100 4 100 100 1
You can also use find
to obtain both the row and column indices for a rectangular matrix, as well as the array values that meet the logical condition. Use the help
facility for more information on find
.
![]() | Relational Operators | Operator Precedence | ![]() |