
How to smoothly interpolate between distant points in 2D matrix?
1 view (last 30 days)
Show older comments
Hey guys,
I have a matrix such as A = [0 0 0 0 0 0;0 5 7 0 9 0;0 0 0 0 0 0;0 0 0 0 0 0;0 0 0 0 0 0;0 0 5 0 0 0;0 3 0 0 6 0;1 0 0 0 0 0]; as I only have some measurements in the area 8x6, points without measurements are zero.
Now I want to interpolate between the points where I have data (>0) using the following:
%if true
[X,Y] = meshgrid(1:1:6,1:1:8);
[Xq,Yq] = meshgrid(1:0.1:6,1:0.1:8);
Vq = interp2(X,Y,A,Xq,Yq);
surf(Xq,Yq,Vq)
%end
This is the product:

The question is, how can I only interpolate between the points with values >0 ("connect the peaks"? If I try to set all zeros as NaN the result is even worse...
Thanks a lot for your help!
Valle
0 Comments
Accepted Answer
John D'Errico
on 1 Apr 2017
Edited: John D'Errico
on 1 Apr 2017
1. Replace the zeros with NaNs.
A(A==0) = NaN
A =
NaN NaN NaN NaN NaN NaN
NaN 5 7 NaN 9 NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN 5 NaN NaN NaN
NaN 3 NaN NaN 6 NaN
1 NaN NaN NaN NaN NaN
B = inpaint_nans(A)
B =
3.1888 5.5122 7.6469 9.0166 9.8404 10.513
3.0913 5 7 8.1801 9 9.8074
3.1825 4.7842 6.3775 7.5284 8.4156 9.2531
3.2633 4.581 5.911 6.9897 7.8907 8.7576
3.1307 4.2808 5.4915 6.4791 7.3464 8.2189
2.6661 3.7807 5 5.8982 6.722 7.5883
1.8903 3 4.1818 5.1491 6 6.8921
1 2.2296 3.4176 4.4402 5.3339 6.2119
surf(B)

More Answers (0)
See Also
Categories
Find more on Interpolation 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!