Main Content

isnan

Determine which array elements are NaN

Description

TF = isnan(A) returns a logical array containing 1 (true) where the elements of A are NaN, and 0 (false) where they are not. If A contains complex numbers, isnan(A) contains 1 for elements with either real or imaginary part is NaN, and 0 for elements where both real and imaginary parts are not NaN.

example

Examples

collapse all

Create a row vector and determine which elements are NaN.

A = 0./[-2 -1 0 1 2]
A = 1×5

     0     0   NaN     0     0

TF = isnan(A)
TF = 1x5 logical array

   0   0   1   0   0

Create an array of complex numbers. Determine whether the complex numbers contain NaN.

A = [2 + 1i, 1/0 + 3i, 1/2 - 1i*NaN]
A = 1×3 complex

   2.0000 + 1.0000i      Inf + 3.0000i   0.5000 +    NaNi

TF = isnan(A)
TF = 1x3 logical array

   0   0   1

Create an array and find the elements with NaN values.

A = [1,3,5,7,NaN,10,NaN,4,6,8]
A = 1×10

     1     3     5     7   NaN    10   NaN     4     6     8

TF = isnan(A)
TF = 1x10 logical array

   0   0   0   0   1   0   1   0   0   0

Index into A with TF to access the elements of A that are NaN. Replace the NaN values with 0.

A(TF) = 0
A = 1×10

     1     3     5     7     0    10     0     4     6     8

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array.

Tips

  • If x is a real scalar, exactly one of isfinite(x), isinf(x), and isnan(x) returns logical 1 (true).

  • For a complex scalar z, isinf(z) and isnan(z) can both return logical 1. For example, isinf(complex(Inf,NaN)) and isnan(complex(Inf,NaN)) both return logical 1.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.

Version History

Introduced before R2006a