the interpolation of a Matrix generated more NaNs elements

I used the following code to perform an interpolation of my matrix , but instead of solving the problem of NaNs entries I have got more because my matrix has got 2 or 3 successive columns of NaNs. Now my question is how to get rid of the NaN s in my matrix without losing data which was available before the interpolation?
[rr,cc] = size(MT);
xi = 1:cc;% columns
idx = ~isnan(MT);
for r = 1:rr
MTNew(r,:) = interp1(xi(idx(r,:)),MT(r,idx(r,:)),xi,'linear','extrap');
end
Thank you !

 Accepted Answer

I cannot reproduce the results you get with my simulated data. Your code works correctly for me with this:
MT = randi(50, 125, 1); % Create Data
MT(randi(125, 30, 1)) = NaN; % Create Data
MT = reshape(MT, 25, 5); % Create Data
[rr,cc] = size(MT);
xi = 1:cc;% columns
idx = ~isnan(MT);
for r = 1:rr
MTNew(r,:) = interp1(xi(idx(r,:)),MT(r,idx(r,:)),xi,'linear','extrap');
end
If your ‘MT’ matrix is not too large, upload it so we can test it with your code.

5 Comments

AndNor’s ‘Answer’ moved here:
Please find attached the matrix MT, Thank you !
Your code worked correctly for me, with no NaN values in the interpolated matrix (in R2015b):
MT = xlsread('AndNor MT.xlsx');
[rr,cc] = size(MT);
xi = 1:cc;% columns
idx = ~isnan(MT);
for r = 1:rr
MTNew(r,:) = interp1(xi(idx(r,:)),MT(r,idx(r,:)),xi,'linear','extrap');
end
figure(1)
subplot(2,1,1)
surf(MT)
grid on
subplot(2,1,2)
surf(MTNew)
grid on
See the plot. In the upper plot note that the NaN values don’t plot. In the lower plot everything plots.
This looks like your code does what you want. If it does not, please describe the problem.
first I appreciate your concern, thank you! actually, I would like to remove the empty spaces in the second plot so I can get a continuous plot, an that was the reason to perform the interpolation in the first place. Do you have any suggestions? Best regards!

There are no empty spaces in the second plot. It (and the matrix that generated it) were complete when I ran your code. The ‘MTNew’ matrix I get is:

MTNew = 
 [-5.9132e+20  -4.7305e+20  -3.5479e+20  -2.3653e+20  -1.1826e+20  -2.2621e+14  -1.0029e+13
  -5.9132e+20  -4.4349e+20  -2.9566e+20  -1.4783e+20   7.3075e+13   7.7769e+13   8.2462e+13
  -1.2368e+22  -1.4148e+13  -1.5963e+15  -3.1785e+15  -4.7607e+15  -2.3767e+15   7.2754e+12
    4.411e+21   1.6233e+14  -1.4787e+15  -3.1197e+15  -4.7607e+15  -2.3684e+15   2.3828e+13
    4.411e+21   4.1926e+12  -1.3355e+14  -2.7129e+14  -4.0904e+14  -5.4678e+14  -6.8452e+14
    -3.8444e+14  -7.5884e+14    1.933e+14
     8.7155e+13   9.1848e+13   9.6542e+13
    -1.7291e+14  -3.5309e+14  -5.3327e+14
    -1.6463e+14  -3.5309e+14    1.933e+14
    -8.2227e+14  -9.6001e+14  -1.0978e+15];
well it seems that my Matlab version is the source of the empty spaces in the plot or if you would like to say the NaNs in MTNew. Thank you for the matrix I will use it. only for persons who will go through this post, I uploaded the matrix that I get using MatLab R2009a , so they can understand this problem easily. Best regards!.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Asked:

Ano
on 2 Feb 2016

Commented:

Ano
on 3 Feb 2016

Community Treasure Hunt

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

Start Hunting!