# MATLAB 中的缺失数据

### 创建并组织缺失数据

MATLAB 中缺失值的形式取决于数据类型。例如，数值数据类型（例如 `double`）使用 `NaN`（非数字）表示缺失值。

`x = [NaN 1 2 3 4];`

`xDouble = [missing 1 2 3 4]`
```xDouble = 1×5 NaN 1 2 3 4 ```
`xDatetime = [missing datetime(2014,1:4,1)]`
```xDatetime = 1x5 datetime NaT 01-Jan-2014 01-Feb-2014 01-Mar-2014 01-Apr-2014 ```
`xString = [missing "a" "b" "c" "d"]`
```xString = 1x5 string <missing> "a" "b" "c" "d" ```
`xCategorical = [missing categorical({'cat1' 'cat2' 'cat3' 'cat4'})]`
```xCategorical = 1x5 categorical <undefined> cat1 cat2 cat3 cat4 ```

`xStandard = standardizeMissing(xDouble,[4 NaN])`
```xStandard = 1×5 NaN 1 2 3 NaN ```

`xSort = sort(xStandard,'MissingPlacement','last')`
```xSort = 1×5 1 2 3 NaN NaN ```

### 查找、替换和忽略缺失数据

```nanData = [1:9 NaN]; plot(1:10,nanData)```

`meanData = mean(nanData)`
```meanData = NaN ```

`TF = isnan(nanData)`
```TF = 1x10 logical array 0 0 0 0 0 0 0 0 0 1 ```

`TFdouble = ismissing(xDouble)`
```TFdouble = 1x5 logical array 1 0 0 0 0 ```
`TFdatetime = ismissing(xDatetime)`
```TFdatetime = 1x5 logical array 1 0 0 0 0 ```

`xTable = table(xDouble',xDatetime',xString',xCategorical')`
```xTable=5×4 table Var1 Var2 Var3 Var4 ____ ___________ _________ ___________ NaN NaT <missing> <undefined> 1 01-Jan-2014 "a" cat1 2 01-Feb-2014 "b" cat2 3 01-Mar-2014 "c" cat3 4 01-Apr-2014 "d" cat4 ```
`TF = ismissing(xTable)`
```TF = 5x4 logical array 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ```

`xFill = fillmissing(xStandard,'constant',0)`
```xFill = 1×5 0 1 2 3 0 ```
`xRemove = rmmissing(xStandard)`
```xRemove = 1×3 1 2 3 ```

`sumNan = sum(xDouble)`
```sumNan = NaN ```
`sumOmitnan = sum(xDouble,'omitnan')`
```sumOmitnan = 10 ```