Accelerating the pace of engineering and science

# Documentation Center

• Trials
• Product Updates

# logical

Convert numeric values to logicals

## Description

example

L = logical(A) converts numeric input A into an array of logical values. Any nonzero element of input 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.

## Examples

expand all

### Pick Odd Elements from Numeric Matrix

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 =

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.

`A(L)`
```ans =

1
5
-3
1
7
3```

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.

`A(~L)`
```ans =

-8
4
2```

## Input Arguments

expand all

### A — Numeric inputscalar | vector | matrix | multidimensional array

Numeric input, specified as a scalar, vector, matrix, or multidimensional array.

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

## More About

expand all

### Tips

• 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.

## See Also

Was this topic helpful?
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}