MATLAB Function Reference
NaN

Not-a-Number

Syntax

• ``````NaN
``````

Description

```NaN ``` returns the IEEE arithmetic representation for Not-a-Number (`NaN`). These result from operations which have undefined numerical results.

Examples

These operations produce `NaN`:

• Any arithmetic operation on a `NaN`, such as `sqrt(NaN)`
• Addition or subtraction, such as magnitude subtraction of infinities as `(+Inf)+(-Inf)`
• Multiplication, such as `0*Inf`
• Division, such as `0/0` and `Inf/Inf`
• Remainder, such as `rem(x,y)` where `y` is zero or `x` is infinity

Remarks

Because two `NaN`s are not equal to each other, logical operations involving `NaN`s always return false, except ~= (not equal). Consequently,

• ````NaN` ~= `NaN`
ans =
1
`NaN` == ```NaN
```ans =
0
```

and the `NaN`s in a vector are treated as different unique elements.

• ```unique([1 1 NaN NaN])
ans =
1 NaN NaN
```

Use the `isnan` function to detect `NaN`s in an array.

• ```isnan([1 1 NaN NaN])
ans =
0     0     1     1
```

`Inf`, `isnan`