MATLAB Fundamentals>>>Fill Missing Values

发布时间:2024年01月20日

MATLAB Fundamentals>Preprocessing Data>Interpolating Missing Data> (1/4) Fill Missing Values

This code sets up the activity.

x = [0 NaN 7 8 NaN 2 -3 NaN -8]
plot(x,"s-","LineWidth",1.5)

任务1:

Create a vector?y?that uses the nearest value to interpolate the?NaN?values of?x.

Display the results by making sure?Cleaned?data?and?Fill?missing?entries?are checked.

解答:

% Fill missing data
[y,missingIndices] = fillmissing(x,"nearest");

% Display results
figure

% Plot cleaned data
plot(y,"SeriesIndex",1,"LineWidth",1.5,"DisplayName","Cleaned data")
hold on

% Plot filled missing entries
plot(find(missingIndices),y(missingIndices),".","MarkerSize",12, ...
    "SeriesIndex",2,"DisplayName","Filled missing entries")
title("Number of filled missing entries: " + nnz(missingIndices))

hold off
legend
clear missingIndices

任务2:

Look at the generated code. Copy and adapt it to create a vector?z?that uses?"linear"?interpolation to replace the?NaN?values of?x.

解答:

% Fill missing data
[z,missingIndices] = fillmissing(x,"linear");

任务3:

Plot?z?with?"*"?markers and a solid line. Add this plot to the existing figure.

解答:

hold on 
plot(z,"*-")
hold off

笔记:注意示例中画图的方法。

文章来源:https://blog.csdn.net/syluxhch/article/details/135722047
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。