Main Content

isnan

Determine which array elements are NaN

Syntax

Description

example

TF= isnan(A)returns a logical array containing1(true) where the elements ofAareNaN, and0(false) where they are not. IfAcontains complex numbers,isnan(A)contains1for elements with either real or imaginary part isNaN, and0元素,真实的和imaginary parts are notNaN.

Examples

collapse all

Create a row vector and determine which elements areNaN.

A = 0./[-2 -1 0 1 2]
A =1×50 0 NaN 0 0
TF = isnan(A)
TF =1x5 logical array0 0 1 0 0

Create an array of complex numbers. Determine whether the complex numbers containNaN.

A = [2 + 1i, 1/0 + 3i, 1/2 - 1i*NaN]
A =1×3 complex2.0000 + 1.0000i Inf + 3.0000i NaN + NaNi
TF = isnan(A)
TF =1 x3逻辑阵列0 0 1

Create an array and find the elements withNaNvalues.

A = [1,3,5,7,NaN,10,NaN,4,6,8]
A =1×1013 5 7 NaN 10 NaN 4 6 8
TF = isnan(A)
TF =1x10 logical array0 0 0 0 1 0 1 0 0 0

Index intoAwithTFto access the elements ofAthat areNaN. Replace theNaNvalues with 0.

A(TF) = 0
A =1×1013 5 7 0 10 0 4 6 8

Input Arguments

collapse all

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

Tips

  • Ifxis a real scalar, exactly one ofisfinite(x),isinf(x), andisnan(x)returns logical1(true).

  • For a complex scalarz,isinf(z)isnan(z)can both return logical 1. For example,isinf(complex(Inf,NaN))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 Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.

Introduced before R2006a