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
笔记:注意示例中画图的方法。