MATLAB Answers

HD
0

surface interpolation based on x/y/diagonals data

Asked by HD
on 12 Feb 2019
Latest activity Commented on by HD
on 12 Feb 2019
Hi,
is it feasable to interpolate data if we only have the x, y cross sections and the diagonals?
exp:
a =
10 0 0 0 0 10 0 0 0 0 10
0 9 0 0 0 9 0 0 0 9 0
0 0 8 0 0 8 0 0 8 0 0
0 0 0 7 0 7 0 7 0 0 0
0 0 0 0 6 6 6 0 0 0 0
10 9 8 7 6 5 6 6 8 9 10
0 0 0 0 6 6 6 0 0 0 0
0 0 0 7 0 7 0 7 0 0 0
0 0 8 0 0 8 0 0 8 0 0
0 9 0 0 0 9 0 0 0 9 0
10 0 0 0 0 10 0 0 0 0 10
can I interpolate the zero data?
I tried griddedInterpolant, interpn, griddata but it doesn't work.
Thank you

  0 Comments

Sign in to comment.

1 Answer

Answer by John D'Errico
on 12 Feb 2019
Edited by John D'Errico
on 12 Feb 2019
 Accepted Answer

By far the simplest solution is to use inpaint_nans. It will produce a surface, interpolating those empty areas. (However, you saidthat you tried tools like griddata, etc. If you used them properly for this problem, griddata or scatteredInterpolant would both have worked.
Just insert a NaN element where ever you lack data. I'dcreate the array as full of NaNs, then insert your datainto the known elements. Or, in thiscase, I'll just replace all zero elements with a NaN. Then call inpaint_nans.
a = [ 10 0 0 0 0 10 0 0 0 0 10
0 9 0 0 0 9 0 0 0 9 0
0 0 8 0 0 8 0 0 8 0 0
0 0 0 7 0 7 0 7 0 0 0
0 0 0 0 6 6 6 0 0 0 0
10 9 8 7 6 5 6 7 8 9 10
0 0 0 0 6 6 6 0 0 0 0
0 0 0 7 0 7 0 7 0 0 0
0 0 8 0 0 8 0 0 8 0 0
0 9 0 0 0 9 0 0 0 9 0
10 0 0 0 0 10 0 0 0 0 10];
a(a == 0) = NaN;
The different methods will produce slightly different behaviior. Note that as you have constructed the fake data here, if the interpolation were to produce a linear interpolation across those holes, then you must get sharp creases along the diagonals.
b = inpaint_nans(a,2);
Whereas an interpolation that tries to be smooth will produce scallops along the sides of the array when plotted as a surface.
b = inpaint_nans(a,0);
Both surfaces produced make sense.

  3 Comments

Thank you very much. you codes is working!
Just for general understanding, this is how I am using gridda:
x=linspace(-5,5,11);
y=x;
[xx,yy]=meshgrid(x,y);
b=griddata(x,y,a,xx,yy,'cubic');
if I replace the zeros by nan b would be nan
can you please comment why it is not working?
Thank you!
It fails in either case, because you are providing the elements to be interpolated as actual data, even in the holes.
How can griddata possibly know that what you give it as zero is to be interpolated? The last time I checked, zero is a number!!!!!!! Anyway, griddata returns the original array, because every data point is already known. griddata is an interpolant. If you just evaluate the surface at the original points, then of course, it just returns the datayou passed in. There should be absolutely no surprise in this.
Next, what happens when you replaced the holes with NaN? Again, griddata does not look at NaN, and remove them. In fact, a NaN is treated as a number by griddata. griddata interpolates numbers. But the problem is, NaNs propagate. Every time you do an interpolation that has a NaN in it, those NaNs will propagate, breeding to produce more NaNs. (Well, breeding is a poor choice of word, but it effectively happens. They spread out.)
What you needed to do, to make griddata work, is to REMOVE all the data points that were not known. Pass in ONLY the acattered points to griddata that were known. Only THEN use griddata to interpolate.
Many thanks for the explanation, that is realy helpful. Now that it works, I wonder what I did! Thanks again for your code.

Sign in to comment.