NaN

Create array of all NaN values

Description

X = NaN returns the scalar representation of "not a number". Operations return NaN when they have undefined numeric results, such as 0/0 or 0*Inf.

example

X = NaN(n) returns an n-by-n matrix of NaN values.

example

X = NaN(sz1,...,szN) returns an sz1-by-...-by-szN array of NaN values, where sz1,...,szN indicate the size of each dimension. For example, NaN(3,4) returns a 3-by-4 matrix.

example

X = NaN(sz) returns an array of NaN values, where the size vector sz defines size(X). For example, NaN([3 4]) returns a 3-by-4 matrix.

example

X = NaN(___,typename) returns an array of NaN values of data type typename, which can be either 'single' or 'double'.

example

X = NaN(___,'like',p) returns an array of NaN values of the same data type, sparsity, and complexity (real or complex) as p. You can specify typename or 'like' but not both.

Examples

collapse all

Create a 3-by-3 matrix of NaN values.

X = NaN(3)
X = 3×3

NaN   NaN   NaN
NaN   NaN   NaN
NaN   NaN   NaN

Create a 2-by-3-by-4 array of NaN values and display its size.

X = NaN(2,3,4);
size(X)
ans = 1×3

2     3     4

Create an array of NaN values that is the same size as an existing array.

A = [1 4; 2 5; 3 6];
sz = size(A);
X = NaN(sz)
X = 3×2

NaN   NaN
NaN   NaN
NaN   NaN

It is a common pattern to combine the previous two lines of code into a single line.

X = NaN(size(A));

Create a 1-by-3 vector of NaN values whose elements are of type single.

X = NaN(1,3,'single')
X = 1x3 single row vector

NaN   NaN   NaN

You can also specify the output type based on the type of another variable. Create a variable p of type single. Then, create a vector of NaN values with the same size and type as p.

p = single([1 2 3]);
X = NaN(size(p),'like',p)
X = 1x3 single row vector

NaN   NaN   NaN

Input Arguments

collapse all

Size of square matrix, specified as an integer.

• If n is 0, then X is an empty matrix.

• If n is negative, then it is treated as 0.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size of each dimension in a list, specified as separate integer arguments.

• If the size of any dimension is 0, then X is an empty array.

• If the size of any dimension is negative, then it is treated as 0.

• Beyond the second dimension, NaN ignores trailing dimensions of length 1. For example, NaN(3,1,1) creates a 3-by-1 vector of NaN values.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size of each dimension in a vector, specified as a row vector of integers.

• If the size of any dimension is 0, then X is an empty array.

• If the size of any dimension is negative, then it is treated as 0.

• Beyond the second dimension, NaN ignores trailing dimensions of length 1. For example, NaN([3 1 1]) creates a 3-by-1 vector of NaN values.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Data type to create, specified as 'double' or 'single'.

Prototype of array to create, specified as an array.

Data Types: double | single
Complex Number Support: Yes

Tips

• X = NaN returns the scalar, type double, IEEE® representation of "not a number". The exact bit-wise hexadecimal representation of this value is fff8000000000000. MATLAB® preserves the "not a number" status of alternate NaN representations and treats all representations equivalently. In some special cases, due to hardware limitations for example, MATLAB does not preserve the exact bit pattern of the alternate representations during computation, and instead uses the canonical NaN bit pattern previously described.

• NaN values are not equal to each other. As a result, comparison operations involving NaN return false, except for the not equal operator ~=. For example, NaN == NaN returns logical 0 (false) but NaN ~= NaN returns logical 1 (true).

• NaN values in a vector are treated as different unique elements. For example, unique([1 1 NaN NaN]) returns the row vector [1 NaN NaN].

• Use the isnan or ismissing function to detect NaN values in an array. The rmmissing function detects and removes NaN values, and the fillmissing function detects NaN values and replaces them with non-NaN values.