MATLAB: The interpolation of a Matrix generated more NaNs elements

interpolationnan s entries

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 !

Best 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.