Main Content

Logical Operations

True or false (Boolean) conditions

The logical data type represents true or false states using the numbers 1 and 0, respectively. Certain MATLAB® functions and operators return logical values to indicate fulfillment of a condition. You can use those logical values to index into an array or execute conditional code. For more information, see how to Find Array Elements That Meet a Condition.

Functions

Short-circuit &&, ||Logical operations with short-circuiting
&Find logical AND
~Find logical NOT
|Find logical OR
xorFind logical exclusive-OR
allDetermine if all array elements are nonzero or true
anyDetermine if any array elements are nonzero
falseLogical 0 (false)
findFind indices and values of nonzero elements
islogicalDetermine if input is logical array
logicalConvert numeric values to logicals
trueLogical 1 (true)

Topics

Find Array Elements That Meet a Condition

This example shows how to filter the elements of an array by applying conditions to the array.

Reduce Logical Arrays to Single Value

This example shows how to use the any and all functions to reduce an entire array to a single logical value.

Operator Precedence

Precedence rules determine the order in which MATLAB evaluates an expression.