MATLAB Function Reference
isreal

Determine if all array elements are real numbers

Syntax

• ```tf = isreal(A)
```

Description

```tf = isreal(A) ``` returns logical false (0) if any element of array `A` has an imaginary component, even if the value of that component is 0. It returns logical true (1) otherwise.

`~isreal(x)` returns logical true for arrays that have at least one element with an imaginary component. The value of that component may be `0`.

 Note    If `a` is real, `complex(a)` returns a complex number whose imaginary component is `0`, and `isreal(complex(a))` returns false. In contrast, the addition `a + 0i` returns the real value `a`, and `isreal(a + 0i)` returns true.

Because MATLAB supports complex arithmetic, certain of its functions can introduce significant imaginary components during the course of calculations that appear to be limited to real numbers. Thus, you should use `isreal` with discretion.

Examples

Example 1. These examples use `isreal` to detect presence or absence of imaginary numbers in an array. Let

• ```x = magic(3);
y = complex(x);
```

`isreal(x)` returns true because no element of `x` has an imaginary component.

• ```isreal(x)

ans =
1
```

`isreal(y)` returns false, because every element of `x` has an imaginary component, even though the value of the imaginary components is `0`.

• ```isreal(y)

ans =
0
```

This expression detects strictly real arrays, i.e., elements with `0`-valued imaginary components are treated as real.

• ``````~any(imag(y(:)))
```
ans =
1
```

Example 2. Given the following cell array,

• ```C{1,1} = pi;
C{1,2} = 'John Doe';
C{1,3} = 2 + 4i;
C{1,4} = ispc;
C{1,5} = magic(3);
C{1,6} = complex(5,0)

C =
[3.1416]  'John Doe'  [2.0000+ 4.0000i]  [1]  [3x3 double]  [5]
```

`isreal` shows that all but `C{1,3}` and `C{1,6}` are real arrays.

• ```for k = 1:6
x(k) = isreal(C{1,k});
end

x

x =
1     1     0     1     1    0
```

`complex`, `isnumeric`, `isnan`, `isprime`, `isfinite`, `isinf`, `isa`, is*