How to keep NaNs in interp?

51 次查看(过去 30 天)
I have an array that contains both NaNs and values. I'm plotting this values to compare to another dataset and if there is a long string of NaNs, interp simply ignores them and interpolates between the point before the first NaN and the point after the last NaN. This kind of throws off my comparison and I would rather have more NaNs so it's blank in the plot to show that no data was not found. Below is the code I used to interpolate array u (array u is the picture shown below). After I interpolated it the warning sign below showed. In the plot I attached the red line is the plot after it has been interpolated. I do not want a constant line like that, but rather an empty space wherever a NaN is, but the NaNs are removed during the interpolation. Is there a way to just interpolate the values and just increase the number of NaNs in the new interpolation? The picture with the datenums is the tarray. tnew is a little large, but I have inserted the code that creates it.
N = (numel(tarray)-1)*359 + numel(tarray);
t_new = linspace(tarray(1),tarray(end),N).';
u_newp = interp1(tarray,u,t_new,'pchip');
plot(t_new,u_new,'-b')
hold on
plot(t_new,u_newp,'-r')
title(sprintf('Path Comparasion of %s', baseFileName))
xlabel('Time')
ylabel('East Velocity m/s')
hold off
datetick('x','HH:MM:SS','keepticks')
legend('HF Data','Hycom Prediction')
Warning: Columns of data containing NaN values have been ignored during interpolation.
> In interp1>Interp1DStripNaN (line 241)
In interp1 (line 171)
In PathComp (line 119)
  4 个评论
Steven Lord
Steven Lord 2017-1-26
So what you've shown is u? Can you also show tarray and t_new?
Sancheet Hoque
Sancheet Hoque 2017-1-26
yea the array with the NaNs is u, while the datenum one I just inserted is the tarray. tnew is 5671x1 double array

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2017-1-26
编辑:Matt J 2017-1-26
Couldn't you just put the NaNs back in u_newp?
u_newp(idx)=nan;
  2 个评论
Matt J
Matt J 2017-1-26
编辑:Matt J 2017-1-26
To obtain the locations 'idx', I would interpolate without 'pchip'. This is NaN preserving,
u_newp = interp1(tarray,u,t_new,'pchip');
idx = isnan(interp1(tarray,u,t_new));
u_newp(idx)=nan;
Sancheet Hoque
Sancheet Hoque 2017-1-26
Thank you! It worked perfectly.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by