主要内容

Missing Data in MATLAB

Working with missing data is a common task in data preprocessing. Although sometimes missing values signify a meaningful event in the data, they often represent unreliable or unusable data points. In either case, MATLAB® has many options for handling missing data.

Create and Organize Missing Data

MATLAB中缺少值所采用的形式取决于数据类型。例如,数字数据类型,例如double利用NaN(not a number) to represent missing values.

x = [NaN 1 2 3 4];

You can also use themissing表示缺少数字数据或其他类型的数据的价值,例如约会时间,string, 和分类。MATLAB自动转换missingvalue to the data's native type.

xDouble = [缺少1 2 3 4]
xDouble =1×5NaN 1 2 3 4
xDatetime = [missing datetime(2014,1:4,1)]
xDatetime =1x5 datetimeNaT 01-Jan-2014 01-Feb-2014 01-Mar-2014 01-Apr-2014
xString = [missing“一个”“ B”“C”“ D”]
xString =1x5 string<缺少>“ a”“ b”“ c”“ d”
xCategorical = [缺少分类({{'cat1''cat2''cat3''cat4'})]
xCategorical =1x5 categorical<定义> cat1 cat2 cat3 cat4

A data set might contain values that you want to treat as missing data, but are not standard MATLAB missing values in MATLAB such asNaN。You can use thestandardizeMissingfunction to convert those values to the standard missing value for that data type. For example, treat 4 as a missingdoublevalue in addition toNaN

Xstandard =标准化(xDouble,[4 nan])
xStandard =1×5NaN 1 2 3 NaN

Suppose you want to keep missing values as part of your data set but segregate them from the rest of the data. Several MATLAB functions enable you to control the placement of missing values before further processing. For example, use the'MissingPlacement'option with the种类function to moveNaNs到数据的末尾。

xSort = sort(xStandard,'MissingPlacement','last')
xSort =1×51 2 3 Nan Nan

查找,替换和忽略缺少的数据

Even if you do not explicitly create missing values in MATLAB, they can appear when importing existing data or computing with the data. If you are not aware of missing values in your data, subsequent computation or analysis can be misleading.

For example, if you unknowingly plot a vector containing aNaN价值,NaN没有出现,因为plotfunction ignores it and plots the remaining points normally.

nandata = [1:9 nan];情节(1:10,Nandata)

Figure contains an axes object. The axes object contains an object of type line.

但是,如果计算数据的平均值,结果是NaN。在这种情况下,事先知道数据包含一个NaN, 和then choose to ignore or remove it before computing the average.

sundata =平均值(nandata)
meanData = NaN

One way to findNaNs in data is by using theisnanfunction, which returns a logical array indicating the location of anyNaNvalue.

TF = isnan(nanData)
TF =1x10逻辑数组0 0 0 0 0 0 0 0 0 1 1

Similarly, theismissingfunction returns the location of missing values in data for multiple data types.

tfdouble =ismissing(xDouble)
tfdouble =1x5 logical array1 0 0 0 0
TFdatetime = ismissing(xDatetime)
TFdatetime =1x5 logical array1 0 0 0 0

假设您正在使用由具有多种数据类型的变量组成的表或时间表。您可以通过一个呼叫找到所有缺失值ismissing,无论他们的类型如何。

Xtable = table(xdouble',xdateTime',xstring',xcategorical')
xTable =5×4桌var1 var2 var3 var4 ____ ___________ _________ ______________________________________________________________________ 1 01-JAN-2014“ A”-2014“ D” CAT4
TF = ismissing(xTable)
TF =5 x4逻辑阵列1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Missing values can represent unusable data for processing or analysis. Usefillmissingto replace missing values with another value, or usermmissing完全删除缺失值。

xFill = fillmissing(xStandard,'constant',0)
xFill =1×50 1 2 3 0
xremove =rmmissing(xStandard)
xremove =1×31 2 3

Many MATLAB functions enable you to ignore missing values, without having to explicitly locate, fill, or remove them first. For example, if you compute the sum of a vector containingNaNvalues, the result isNaN。However, you can directly ignoreNaN通过使用'omitnan'option with thesumfunction.

sumNan = sum(xDouble)
sumnan = nan
sumOmitnan = sum(xDouble,'omitnan')
sumOmitnan = 10

See Also

|||

相关话题