Accelerating the pace of engineering and science

Documentation Center

• Trial Software

Overview of the Logical Class

The logical data type represents a logical true or false state using the numbers 1 and 0, respectively. Certain MATLAB® functions and operators return logical true or false to indicate whether a certain condition was found to be true or not. For example, the statement 50>40 returns a logical true value.

Logical data does not have to be scalar; MATLAB supports arrays of logical values as well. For example, the following statement returns a vector of logicals indicating false for the first two elements and true for the last three:

```[30 40 50 60 70] > 40
ans =
0     0     1     1     1
```

This statement returns a 4-by-4 array of logical values:

```x = magic(4) >= 9
x =
1     0     0     1
0     1     1     0
1     0     0     1
0     1     1     0
```

The MATLAB functions that have names beginning with is (e.g., ischar, issparse) also return a logical value or array:

```a = [2.5 6.7 9.2 inf 4.8];

isfinite(a)
ans =
1     1     1     0     1
```

Logical arrays can also be sparse as long as they have no more than two dimensions:

```x = sparse(magic(20) > 395)
x =
(1,1)        1
(1,4)        1
(1,5)        1
(20,18)       1
(20,19)       1
```