Main Content

ismissing

Find missing values

Description

example

TF = ismissing(A)returns a logical array that indicates which elements of the input data contain missing values. The size ofTFis the same as the size ofA.

Missing values are defined according to the data type ofA:

  • NaNdouble,single,duration, andcalendarDuration

  • NaTdatetime

  • string

  • categorical

  • ' 'char

  • {''}cellof character vectors

IfAis a table, then the data type of each column defines the missing value for that column.

For data types with no default definition of a standard missing value,ismissing(A)returns an array or table of logical0(false) values the same size asA.

example

TF = ismissing(A,indicator)treats the values inindicatoras missing value indicators, ignoring all default indicators listed in the previous syntax.indicatorcan be a single indicator or multiple indicators. For example, ifAis an array of typedouble, thenismissing(A,[0,-99])treats 0 and –99 as missingdoublevalues instead ofNaN.

TF= ismissing(___'OutputFormat',format)specifies for table or timetable input data the output data type for any of the previous syntaxes.

Examples

collapse all

Create a row vectorAthat containsNaNvalues, and identify their locations inA.

A = [3 NaN 5 6 7 NaN NaN 9]; TF = ismissing(A)
TF =1x8 logical array0 1 0 0 0 1 1 0

Create a table with variables of different data types.

dblVar = [NaN;3;5;7;9;11;13]; singleVar = single([1;NaN;5;7;9;11;13]); cellstrVar = {'one';'three';'';'seven';'nine';'eleven';'thirteen'}; charVar = ['A';'C';'E';' ';'I';'J';'L']; categoryVar = categorical({'red';'yellow';'blue';'violet';'';'ultraviolet';'orange'}); dateVar = [datetime(2015,1:2:10,15) NaT datetime(2015,11,15)]'; stringVar = ["a";"b";"c";"d";"e";"f";missing]; A = table(dblVar,singleVar,cellstrVar,charVar,categoryVar,dateVar,stringVar)
A=7×7 tabledblVar singleVar cellstrVar charVar categoryVar dateVar stringVar ______ _________ ____________ _______ ___________ ___________ _________ NaN 1 {'one' } A red 15-Jan-2015 "a" 3 NaN {'three' } C yellow 15-Mar-2015 "b" 5 5 {0x0 char } E blue 15-May-2015 "c" 7 7 {'seven' } violet 15-Jul-2015 "d" 9 9 {'nine' } I  15-Sep-2015 "e" 11 11 {'eleven' } J ultraviolet NaT "f" 13 13 {'thirteen'} L orange 15-Nov-2015 

Find the elements with missing values.

ismissingreturns an array that has a logical 1 for every corresponding element inAwith a missing value. The size ofTFis the same as the size ofA.

TF = ismissing(A)
TF =7x7 logical array1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1

Create a table where'NA','',-99,NaN, andInfrepresent missing values. Then, find the elements with missing values.

dblVar = [NaN;3;Inf;7;9]; int8Var = int8([1;3;5;7;-99]); cellstrVar = {'one';'three';'';'NA';'nine'}; charVar = ['A';'C';'E';' ';'I']; A = table(dblVar,int8Var,cellstrVar,charVar)
A=5×4 tabledblVar int8Var cellstrVar charVar  ______ _______ __________ _______ NaN 1 {'one' } A 3 3 {'three' } C Inf 5 {0x0 char} E 7 7 {'NA' } 9 -99 {'nine' } I

Specify the missing value indicators.ismissingreturns an array that has a logical 1 for every corresponding element inAwith a missing value.

id = {'NA'''-99 NaN Inf}; TF = ismissing(A,id)
TF =5x4 logical array1 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 0 0

ismissingignores trailing white space in character arrays. Therefore, because the empty character vector,'', is specified as a missing value indicator,ismissingidentifies the empty character vector inA.cellstrVarand also the blank space inA.charVaras missing values.

Input Arguments

collapse all

Input data, specified as a vector, matrix, multidimensional array, table, or timetable.

  • IfAis a timetable, thenismissingoperates on the table data only and ignoresNaTandNaNvalues in the vector of row times.

  • IfAis a cell array or a table with cell array variables, thenismissingonly detects missing elements when the cell array contains character vectors.

Missing value indicator, specified as a scalar, vector, or cell array.

  • IfAis an array, thenindicatormust be a vector.

  • IfAis a table or timetable, thenindicatorcan also be a cell array with entries of multiple data types.

The entries ofindicatorindicate the values thatismissingtreats as missing and overrides all default standard missing indicators. If you want to add indicators while maintaining the list of standard indicators, then you must include all default indicators as elements ofindicator. For example, ifAis a table withcategoricaland numeric values, useismissing(A,{-99,''})to indicate-99as a missing numeric value, but preserveas a missingcategoricalvalue.

You can also use themissingvalue as an indicator for any missing data represented asNaN,NaT,missing, or. IfAis a table, thenmissingis also an indicator for missing character vectors (' ') and missing cell arrays of character vectors ({''}).

Indicator data types match data types in the entries ofA. These are additional data type matches between the indicator and elements ofA:

  • doubleindicators matchdouble,single, integer, andlogicalentries ofA.

  • stringandcharindicators, and indicators that are cell arrays of character vectors, matchstringentries ofA.

  • stringandcharindicators matchcategoricalentries ofA.

Example:TF = ismissing(A,0)recognizes only0as a missing value.

Output data type, specified as one of these values:

  • 'logical'— For table or timetable input data, return the outputTFas a logical array.

  • 'tabular'— For table input data, return the outputTFas a table. For timetable input data, return the outputTFas a timetable.

For vector, matrix, or multidimensional array input data,OutputFormatis not supported.

Example:ismissing(T,'OutputFormat','tabular')

Tips

  • Because integer variables cannot storeNaN,使用一个特殊的整数值(否则未使用)to indicate missing integer data, such as-99.

  • For more information on finding missing strings, seeTest for Empty Strings and Missing Values.

Algorithms

ismissinghandles leading and trailing white space differently for indicators that are cell arrays of character vectors, character arrays, or categorical arrays.

  • For cell arrays of character vectors,ismissingdoes not ignore indicator white space. All character vectors must match exactly.

  • For character arrays in table variables,ismissingignores trailing white space in the indicator.

  • For categorical arrays,ismissingignores leading and trailing white space in the indicator.

Extended Capabilities

Version History

Introduced in R2013b

expand all

Behavior changed in R2022a