Matlab自学笔记四十八:各类型缺失值的创建、判断、替换、移位和处理方法

发布于:2025-03-17 ⋅ 阅读:(12) ⋅ 点赞:(0)

1.各类数据缺失值的创建

程序示例如下:

a=[nan 1 2 3]  %数值型缺失值

s=[string(missing) "a" "b"]  %字符串型缺失值

t=[NaT datetime(2018,8,8)]  %时间型缺失值

isnan(a)  %判断数值型缺失值

运行结果:

a =

   NaN     1     2     3

s =

  1×3 string 数组

    <missing>    "a"    "b"

t =

  1×2 datetime 数组

   NaT          2018-08-08

ans =

  1×4 logical 数组

   1   0   0   0

2. missing函数可创建各种类型的缺失值

程序示例如下:

aa=[missing 1 2 3] %数值型缺失值

ss=[missing "a" "b"] %字符串型缺失值

tt=[missing datetime(2018,8,8)] %时间型缺失值

ismissing(a)  %使用ismissing判断各类型缺失值

ismissing(s)

ismissing(t)

运行结果:

aa =

   NaN     1     2     3

ss =

  1×3 string 数组

    <missing>    "a"    "b"

tt =

  1×2 datetime 数组

   NaT          2018-08-08

ans =

  1×4 logical 数组

   1   0   0   0

ans =

  1×3 logical 数组

   1   0   0

ans =

  1×2 logical 数组

   1   0

3.缺失值的替换

缺失值替换使用函数standardizeMissing,缺失值替换为使用函数fillmissing,程序示例如下:

standardizeMissing(a,[2 missing])  %变量中参数2替换为缺失值

standardizeMissing(s,["b" missing])  %变量中参数"b"替换为缺失值

standardizeMissing(t,[datetime(2018,8,8) missing])

fillmissing(a,'constant',0)  %变量中缺失值替换成参数0,'constant'和0表示把缺失值替换为常数0

fillmissing(s,'constant',"fill") %变量中缺失值替换成参数"fill"

fillmissing(t,'constant',datetime(2019,9,9))

运行结果:

ans =

   NaN     1   NaN     3

ans =

  1×3 string 数组

    <missing>    "a"    <missing>

ans =

  1×2 datetime 数组

   NaT   NaT

ans =

     0     1     2     3

ans =

  1×3 string 数组

    "fill"    "a"    "b"

ans =

  1×2 datetime 数组

   2019-09-09   2018-08-08

4.缺失值的移位(排序)

sort(a,'MissingPlacement','last')  %把变量a中的缺失值移位到最后

运行结果:

ans =

     1     2     3   NaN

5.缺失值的运算

max(a)  %忽略nan求最大值

sin(a)  %nan的sin值就是nan

sum(a)  %求和返回nan值

sum(a,'omitnan')  %忽略nan

sum(rmmissing(a))  %移除a中的缺失值

运行结果:

ans =

     3

ans =

       NaN    0.8415    0.9093    0.1411

ans =

   NaN

ans =

     6

ans =

     6

相关视频内容:

1.42/Matlab缺失数据的处理/判断/替换/忽略/移除

38.4 从图片上自动提取圆心坐标和半径数据

26.5 SVM支持向量机,核函数Kernel和核变换,Matlab编程实例

37.8 Matlab Appdesigner界面编程实例:闹铃/定时器,timer定时原理和程序应用

22.9 混合整数线性规划intlinprog变量取值约束是整数

24.10 训练交叉验证模型,crossval,KFold,CVPartition

37.15 Matlab app多功能图像函数演示界面,Appdesigner工具栏的用法,多个图像绘图回调函数的原理和应用

23.3 详解拟合优度评价指标:sse,rmse,dfe,rsquare,Adjrsquare

66.44 GUI/APP界面设计如何调用外部m文件进行运算并显示结果和图像(M文件GUI界面化)

End