How to keep NaNs in interp?

조회 수: 23 (최근 30일)
Sancheet Hoque
Sancheet Hoque 2017년 1월 26일
편집: Matt J 2017년 1월 26일
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