bug with interp2 ?
8 次查看(过去 30 天)
显示 更早的评论
Hello,
I want to interpolate a 2D data "v" from a 2km latitude and longitude grid (xx and yy) to a 10 km grid (Xq and Yq). but with interp2 I get the following error :
>> [Xq,Yq] = ndgrid(lon_extraction,lat_extraction); %
>> whos xx yy v X Y
Name Size Bytes Class Attributes
X 23x83 15272 double
Y 23x83 15272 double
v 407x793 2582008 double
xx 407x793 2582008 double
yy 407x793 2582008 double
>> h_extr_interp = interp2(xx,yy, v, X,Y);
Error using griddedInterpolant
Grid arrays must have NDGRID structure.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 134)
F = makegriddedinterp(X, Y, V, method,extrap);
>>
(I get the same result using meshgrid instead of ndgrid)
any ideas why I get this message??
3 个评论
Stephen23
2023-5-3
编辑:Stephen23
2023-5-3
"(I get the same result using meshgrid instead of ndgrid)"
Your code shows that you do not use the outputs of NDGRID, so changing the function to MESHGRID will not change that.
"Problem solved using griddata instead of interp2"
If you have data in ND-grid format, then use INTERPN:
The MATLAB documentation explains how to select the operator based on the data arrangement:
Matt J
2023-5-3
Problem solved using griddata instead of interp2
griddata is quite a bit slower than interp2. You should not use it unless you are certain your xx,yy locations don't define a regular lattice.
回答(1 个)
Walter Roberson
2023-5-2
移动:Walter Roberson
2023-5-2
[Xq,Yq] = ndgrid(lon_extraction,lat_extraction);
h_extr_interp = interp2(xx,yy, v, X,Y);
What do xx and yy or X and Y have to do with Xq and Yq ? You build an ndgrid but you do not use it in the interp2
5 个评论
Stephen23
2023-5-3
编辑:Stephen23
2023-5-3
Your data are scattered, they are not on a grid:
S = load('test_bug.mat')
S.xx
S.yy
The rows and columns clearly do not consist of identical values. Therefore your data are not gridded.
Compare against the perfectly gridded sample points returned by NDGRID or MESHGRID:
[X,Y] = meshgrid(0:3,4:7)
So you will need to use a scattered interpolant (not a gridded interpolant) e.g. GRIDDATA:
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!