Convert numeric values to logicals
L = logical(A)
Pick out the odd-numbered elements of a numeric matrix.
Create a numeric matrix.
A = [1 -3 2;5 4 7;-8 1 3];
Find the modulus,
mod(A,2), and convert it to a logical array for indexing.
L = logical(mod(A,2))
L = 3x3 logical array 1 1 0 1 0 1 0 1 1
The array has logical
true) values where
A is odd.
L as a logical index to pick out the odd elements of
ans = 6×1 1 5 -3 1 7 3
The result is a vector containing all odd elements of
Use the logical NOT operator,
L to find the even elements of
ans = 3×1 -8 4 2
A— Input array
Input array, specified as a scalar, vector, matrix, or multidimensional array.
Most arithmetic operations involving logical arrays return double values. For example, adding zero to a logical array returns a double array.
Logical arrays also are created by the relational
etc.) and functions like
This function fully supports tall arrays. For more information, see Tall Arrays.