用nan绘制缺失值

时间:2014-04-24 08:21:24

标签: arrays matlab insert plot nan

我有一个看起来像这样的情节

hours = [0 1 2 3 4 5 6 12 13 14 15];
y = [0 1 2 nan 3 4 5 6 7 8 9];
figure(1)
plot(hours, y, '-o');

enter image description here

正如你所看到的,x轴从6跳到12而不是连接这些点的直线,我想有一个差距:

hours = [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
y = [0 1 2 nan 3 4 5 nan nan nan nan nan 6 7 8 9];
figure(2)
plot(hours, y, '-o');

enter image description here

这可以以优雅的方式完成而无需手动插入nans和新的'hours'值吗?

1 个答案:

答案 0 :(得分:4)

试试这个 -

hours_new = min(hours):max(hours)
nan_ind = ~ismember(hours_new,hours)
y_new(~nan_ind) = y
y_new(nan_ind) = NaN

然后,这样的情节 -

figure(2)
plot(hours_new, y_new, '-o');