This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Convert numeric values to logicals


L = logical(A)



L = logical(A) converts A into an array of logical values. Any nonzero element of A is converted to logical 1 (true) and zeros are converted to logical 0 (false). Complex values and NaNs cannot be converted to logical values and result in a conversion error.


collapse all

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 1 (true) values where A is odd.

Use L as a logical index to pick out the odd elements of A.

ans = 6×1


The result is a vector containing all odd elements of A.

Use the logical NOT operator, ~, on L to find the even elements of A.

ans = 3×1


Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char


  • 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 operators (==,<,>,~, etc.) and functions like any, all, isnan, isinf, and isfinite.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a