bug with interp2 ?
조회 수: 5 (최근 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!