nan

Creates arrays filled with NaN (not a number).

Category: Other public functions: elmat

Syntax

x = nan

The scalar NaN.

x = nan(n)

An n-by-n matrix of NaN.

x = nan(sz1, ..., szN)

An array with the given dimensions.

x = nan(___, classname)

Creates the array as 'double' or 'single'.

x = nan(___, 'like', p)

Uses the class of the variable p.

Description

NaN stands for an undefined numeric result such as 0/0 or Inf - Inf. It is not equal to anything, not even itself, so test for it with isnan, never with ==. Preallocating with nan is a good way to mark entries that have not been computed yet: forgotten cells stay visible instead of looking like zeros.

Most statistics functions propagate NaN; options such as 'omitnan' skip it.

Input arguments

n, sz1, ..., szN
Non-negative integers with the sizes.
classname
'double' (default) or 'single'.

Output arguments

x
Array of NaN values.

Example

NaN placeholders

x = nan(2, 3)
x(1, 2) = 5;
isnan(x)
NaN == NaN
Output
x = 2×3

   NaN   NaN   NaN
   NaN   NaN   NaN
ans = 2×3 logical array

   1   0   1
   1   1   1
ans = logical
   0

See also

inf isnan ismissing fillmissing