Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# NaN

Not-a-Number

## Syntax

NaN
NaN('double')
NaN('single')
NaN(n)
NaN(m,n)
NaN([m,n])
NaN(m,n,p,...)
NaN([m,n,p,...])
NaN(...,'like',p)

## Description

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

NaN('double') is the same as NaN with no inputs.

NaN('single') is the single precision representation of NaN.

NaN(n) is an n-by-n matrix of NaNs.

NaN(m,n) or NaN([m,n]) is an m-by-n matrix of NaNs.

NaN(m,n,p,...) or NaN([m,n,p,...]) is an m-by-n-by-p-by-... array of NaNs.

 Note   The size inputs m, n, p, ... should be nonnegative integers. Negative integers are treated as 0.

NaN(...,classname) is an array of NaNs of class specified by the string classname. classname can be either 'single' or 'double'.

NaN(...,'like',p) is an array of NaNs of the same data type, sparsity, and complexity (real or complex) as the single or double precision numeric variable p.

## 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

## More About

expand all

### Tips

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

NaN ~= NaN
ans =
1
NaN == NaN
ans =
0

and the NaNs in a vector are treated as different unique elements.

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

Use the isnan function to detect NaNs in an array.

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

## See Also

Was this topic helpful?