MATLAB Answers

Skirt Zhang

replace missing value in a matrix using intropolated values

Asked by Skirt Zhang
on 16 Jan 2013

Dear All,

I have a matrix A as below: A =

    1.1093   -0.7697    1.1006   -0.6156    0.4882   -0.8045    0.1049
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
   -1.2141    1.1174   -1.4916    0.8886    1.4193   -0.2437   -0.6669
   -1.1135   -1.0891   -0.7423   -0.7648    0.2916    0.2157    0.1873
   -0.0068       NaN   -1.0616   -1.4023    0.1978   -1.1658   -0.0825
    1.5326    0.5525       NaN   -1.4224    1.5877   -1.1480       NaN

I want to replace the NaN with the values generated from interpolation which can take care of both the interpolation for rows and column. So far I have in mind is take interpolation for row get value a_row; then do it for column get a_column. In the end replace nan with the mean value for the two. Can anyone help me about this? Also, I have problems with specifying y for the function : output(a)= interp1(A,y,'linear');

if I don't know y how should I specify here?

thanks a lot in advance



No products are associated with this question.

1 Answer

Answer by Andrei Bobrov
on 16 Jan 2013
Edited by Andrei Bobrov
on 16 Jan 2013

one way

t = ~isnan(A);
[x,y] = find(t);
F = TriScatteredInterp(x,y,A(t));
[ii,jj] = ndgrid(1:size(A,1),1:size(A,2));
out = F(ii,jj);

ADD variant

out(end) = mean(cellfun(@(x)interp1(1:2,x(1:2),3,'linear','extrap'),...
                       {out(end,end-[2 1 0])', out(end-[2 1 0],end),...
                                out(end - (size(out,1)+1)*(2:-1:0))'}));


Hi Bobrov, thanks for your reply. But now I still have NaNs in the output which are located in the end of the matrix. Do you have any idea to solve this problem

Hi Skirt! See ADD part in my answer.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!