There is a bug with the griddedInterpolant function with nearest and linear methods. If the query set contains any out-of-range values, the entire evaluated set is NaN, not just the results corresponding to the out-of-range queries.
In contrast, the griddedInterpolant with cubic interpolation method returns NaN for only out-of-range queries and correctly evaluates the in-range queries.
This is an problem for me because I'd like to use the linear method. I'm currently filtering out the out-of-range values from the query before processing, but the best solution would be to get the linear griddedInterpolant to work as it should. How should I fix it?
This bug is demonstrated by the following example script:
[X,Y,Z] = ndgrid(1:10,1:10,1:10);
V = X.^.5 + Y.^.5 + Z.^.5;
nearestInterp = griddedInterpolant(X,Y,Z,V,'nearest');
linearInterp = griddedInterpolant(X,Y,Z,V,'linear');
cubicInterp = griddedInterpolant(X,Y,Z,V,'cubic');
splineInterp = griddedInterpolant(X,Y,Z,V,'spline');
[Xq,Yq,Zq] = ndgrid(0:.5:10,0:.5:10,0:.5:10);
Vq_nearest = nearestInterp(Xq,Yq,Zq);
Vq_linear = linearInterp(Xq,Yq,Zq);
Vq_cubic = cubicInterp(Xq,Yq,Zq);
Vq_spline = splineInterp(Xq,Yq,Zq);
Thanks, Nick

댓글 수: 3

Honglei Chen
Honglei Chen 2012년 3월 26일
Hi Nick, which version of MATLAB are you using, I tried this in R2012a and couldn't reproduce it.
Marc Lalancette
Marc Lalancette 2012년 10월 18일
I know this is an old thread but I also ran into this problem, using version 2011b. Is it possible to get this fixed without paying hundreds of dollars for a new version?
Matt J
Matt J 2012년 10월 18일
편집: Matt J 2012년 10월 18일
The best solution, other than upgrading, is probably to create your own wrapper that separates out-of-bounds values from inbound values and only calls griddedInterpolant on the latter. You would do that only when the linear interpolation method is active, of course.

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

 채택된 답변

Sean de Wolski
Sean de Wolski 2012년 10월 18일

0 개 추천

This behavior was fixed in R2012a. If you are current on SMS, you can download R2012a or R2012b for free.

댓글 수: 5

Wen
Wen 2014년 5월 28일
I'm seeing the same problem for spline in R2014a.
Sean de Wolski
Sean de Wolski 2014년 5월 28일
Wen, can you provide a minimal working example?
Wen
Wen 2014년 6월 1일
Sorry, I realized the problem I found was a bit different then the topic question. It has to do with ignoring NaN values in V when making the interpolated surface. If nearest, linear, or cubic are used, I can get the values of the interpolated surface as long as it's not close to the x, y, z values at which V is NaN. However, the spline option makes everything NaN instead of only at those x, y, z values.
I added one line to the one Nick gave:
[X,Y,Z] = ndgrid(1:10,1:10,1:10);
V = X.^.5 + Y.^.5 + Z.^.5;
V(10, 10, 10) = NaN;
nearestInterp = griddedInterpolant(X,Y,Z,V,'nearest');
linearInterp = griddedInterpolant(X,Y,Z,V,'linear');
cubicInterp = griddedInterpolant(X,Y,Z,V,'cubic');
splineInterp = griddedInterpolant(X,Y,Z,V,'spline');
[Xq,Yq,Zq] = ndgrid(0:.5:10,0:.5:10,0:.5:10);
Vq_nearest = nearestInterp(Xq,Yq,Zq);
Vq_linear = linearInterp(Xq,Yq,Zq);
Vq_cubic = cubicInterp(Xq,Yq,Zq);
Vq_spline = splineInterp(Xq,Yq,Zq);
Matt J
Matt J 2014년 6월 2일
편집: Matt J 2014년 6월 2일
That's not a bug, I don't think. With splines, the piecewise polynomial coefficients used to interpolate between the V(i) depend on all V(i) simultaneously. Any NaNs in the given data set can be expected to propagate everywhere.
Wen
Wen 2014년 6월 2일
Ah ok, makes sense. I see cubic has more NaNs in its interpolated values than linear, and it depends on 4 points (or is it 3?) rather than 2. Okay.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

질문:

2012년 3월 25일

댓글:

Wen
2014년 6월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by