Interpolating missing values

조회 수: 12 (최근 30일)
Jenny
Jenny 2011년 11월 3일
답변: Jennifer Rebbin 2023년 9월 20일
I have a matrix with NaNs in some of the cells. I need to find all cells of the matrix with NaN values, then interpolate the missing values using the first value before and after the NaNs, and put the interpolated values in the place of the NaN values within the matrix. NaNs may be only one at a time or several in a row, the first and last value around NaN sections may span more than one row.
I have looked at find and interp1 in Matlab help, but can't make it work for my situation. For example: how do you make find locate NaN values? And with interp1, I am not sure what all the variables should be in this case. Any tips?
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 11월 3일
find(isnan(X))
Jenny
Jenny 2011년 11월 6일
thanks!

댓글을 달려면 로그인하십시오.

채택된 답변

Jennifer Rebbin
Jennifer Rebbin 2023년 9월 20일
Starting in R2023b, you can fill missing entries in 2-D data using the fillmissing2 function.

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 3일
Interpolation on a 1-D vector is not that hard. Maybe it could be expanded to 2-D matrix too. See if interp1() is all you need.
%%original data
x=1:0.1:10;
y=sin(x);
ind=round(ceil(90*rand(10,1)));
y(ind)=nan;
figure(1);plot(x,y);
%processing
ind=~isnan(y);
NewY=interp1(x(ind),y(ind),x);
figure(2);plot(x,NewY);
For 2-D
%%original good data
[x,y,z]=peaks;
figure(1); surf(x,y,z);
%missing some z values
[m,n]=size(z);
ind_m=ceil((m-1)*rand(10,1));
ind_n=ceil((n-1)*rand(10,1));
Incomplete_z=z;
Incomplete_z(ind_m,ind_n)=nan;
figure(2);surf(x,y,Incomplete_z);
%interpolate to recover z
index=isnan(Incomplete_z);
row_index=any(index,2);
col_index=any(index,1);
NewX=x(~row_index,~col_index);
NewY=y(~row_index,~col_index);
NewZ=Incomplete_z(~row_index,~col_index);
Interpolate_z=interp2(NewX,NewY,NewZ,x,y);
figure(3);surf(x,y,Interpolate_z)
  댓글 수: 2
Jenny
Jenny 2011년 11월 6일
I understand what you've done here, but I am not sure how to make it fit if I have a m by n matrix with randomly dispersed nan values in it. I don't know what to set my x,Y, and xi to in the equation yi = interp1(x,Y,xi). I can use find=(isnan(X')) to find all the indeces of the nan's by row, but I don't know where that fits in the equation above.
Fangjun Jiang
Fangjun Jiang 2011년 11월 6일
2-D is similar. You need to get rid of the rows and columns where any element in the row or column is nan. See update.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 11월 6일
You may wish to consider using John D'Errico's File Exchange Contribution inpaint_nans

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by