Interpolation of 2D matrix using Interp2 to eliminate NaN

104 views (last 30 days)
I have a 310*400 matrix, that contain NAN values. I will like to interpolate the data to eliminate the NAN. After applying this code, I observed that the NAN is still retained. A sample of my code is here.
load('km100.dat'); % load the z column data
[x,y] = ndgrid(310,400); % arrange the data into grid
data_nan=reshape(km100,[],400); %
row_vect = 1:310; %
col_vect = 1:400; %
[X,Y] = meshgrid(col_vect, row_vect);
[Xq,Yq] = meshgrid((1:1:310),(1:1:400));
V = data_nan;
data_interp = interp2(X, Y, V, Xq', Yq', 'cubic');
How do I correct it? Thanks.

Accepted Answer

Stephen23
Stephen23 on 25 Apr 2018
Edited: Stephen23 on 25 Apr 2018
"How do I correct it? Thanks."
NaN data is missing data, so although you think that you have gridded data in reality you don't, because some of it is missing. Therefore you need to use some method that can interpolate non-gridded data:
To use: remove NaN values (giving vectors) and then interpolate at the requires grid points.
The easiest solution would be to download John D'Errico's excellent FEX submission, it might do what you want:
  2 Comments
Stephen23
Stephen23 on 25 Apr 2018
Edited: Stephen23 on 25 Apr 2018
I just tried inpaint_nans on your data and it worked as expected:
>> old = load('km100.txt');
>> new = inpaint_nans(old);
>> nnz(isnan(old))
ans = 47270
>> nnz(isnan(new))
ans = 0
>> [old,new]
ans =
452.8535767 452.8535767
453.9577942 453.9577942
453.0496521 453.0496521
455.6515198 455.6515198
452.5939636 452.5939636
452.6958923 452.6958923
454.7310181 454.7310181
448.0514832 448.0514832
443.7440186 443.7440186
442.4247437 442.4247437
447.3446655 447.3446655
464.2363586 464.2363586
458.6205444 458.6205444
451.6611328 451.6611328
448.5695496 448.5695496
450.1520996 450.1520996
448.7554016 448.7554016
450.8416443 450.8416443
438.6665344 438.6665344
433.2290649 433.2290649
447.3460693 447.3460693
455.7644653 455.7644653
455.448822 455.448822
453.618042 453.618042
455.2015076 455.2015076
453.8422852 453.8422852
457.0555725 457.0555725
468.3102112 468.3102112
470.1549683 470.1549683
473.3843079 473.3843079
477.168457 477.168457
466.2926941 466.2926941
477.1495056 477.1495056
467.2353821 467.2353821
474.5125122 474.5125122
477.6864624 477.6864624
480.7120972 480.7120972
474.8183899 474.8183899
471.5950928 471.5950928
468.0055542 468.0055542
475.824585 475.824585
468.5657349 468.5657349
467.0472107 467.0472107
472.8373108 472.8373108
467.2086487 467.2086487
454.8962097 454.8962097
455.7807922 455.7807922
461.6356506 461.6356506
448.2521362 448.2521362
462.8482666 462.8482666
466.4006958 466.4006958
...
141.0991211 141.0991211
72.76783752 72.76783752
37.56958008 37.56958008
95.11649323 95.11649323
NaN 140.0638330942
NaN 173.459108598242
NaN 196.349828667769
NaN 209.783502228422
NaN 214.807638205843
NaN 212.469745525675
NaN 203.817333113559
NaN 189.897909895138
NaN 171.758984796054
NaN 150.448066741949
NaN 127.012664658465
NaN 102.500287471243
NaN 77.9584441059274
NaN 54.4346434881588
NaN 32.9763945435796
NaN 14.631206197832
NaN 0.446587376558134
NaN -8.52995299459991
-11.25090599 -11.25090599
-6.668762684 -6.668762684
-18.93135834 -18.93135834
-44.52261734 -44.52261734
... etc
It worked for me. What did you try?
Ope
Ope on 26 Apr 2018
Many thanks, the function 'inpaint_nans' solved the problem.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!