logical
Converts values to logical true or false.
Category: Language Fundamentals > Operators and Elementary Operations
Syntax
L = logical(A)
Returns an array of true where A is nonzero and false where it is zero.
Description
Any nonzero value, positive or negative, becomes true, so logical([2 0 -1]) is [1 0 1]. The logical class occupies one byte per element and is what comparisons return. It is required for logical indexing: only true/false arrays select elements as a mask, while a numeric array of zeros and ones is read as subscripts.
NaN has no truth value and raises an error.
Input arguments
A- Numeric or logical array, or char. Must not contain
NaN.
Output arguments
L- Logical array of the same size as
A.
Example
m = logical([2 0 -1])
x = [10 20 30];
x(m)
x(logical([0 1 1]))
class(m)
Output
m = 1×3 logical array
1 0 1
ans = 1×2
10 30
ans = 1×2
20 30
ans = 'logical'