surface interpolation based on x/y/diagonals data

조회 수: 3 (최근 30일)
HD
HD 2019년 2월 12일
댓글: HD 2019년 2월 12일
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

채택된 답변

John D'Errico
John D'Errico 2019년 2월 12일
편집: John D'Errico 2019년 2월 12일
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
John D'Errico
John D'Errico 2019년 2월 12일
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.
HD
HD 2019년 2월 12일
Many thanks for the explanation, that is realy helpful. Now that it works, I wonder what I did! Thanks again for your code.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by