# 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 ```
{"ft_lang_label":"__label__zh","ft_lang_prob":0.6376249,"math_prob":0.9969697,"size":2542,"snap":"2023-14-2023-23","text_gpt3_token_len":1448,"char_repetition_ratio":0.14814815,"word_repetition_ratio":0.09815951,"special_character_ratio":0.3115657,"punctuation_ratio":0.041162226,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9958376,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-25T02:19:30Z\",\"WARC-Record-ID\":\"<urn:uuid:2f055891-6073-458b-be37-8e58c5532006>\",\"Content-Length\":\"86755\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3ea6dfe3-6aa3-47be-b830-e1ce1af94f0d>\",\"WARC-Concurrent-To\":\"<urn:uuid:d65f913f-85b5-443f-99d8-7b6db628ca4d>\",\"WARC-IP-Address\":\"23.208.34.77\",\"WARC-Target-URI\":\"https://ww2.mathworks.cn/help/matlab/data_analysis/missing-data-in-matlab.html\",\"WARC-Payload-Digest\":\"sha1:YZVGH532MKCI62XHOQCFXYEUZE3GX3KP\",\"WARC-Block-Digest\":\"sha1:OIL652UNS5QX52T2Y6W6NE5Q5GPU6V6Y\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296945292.83_warc_CC-MAIN-20230325002113-20230325032113-00074.warc.gz\"}"}