Error using interp3 "Error using griddedInterpolant. Grid arrays must have NDGRID structure"
Afficher commentaires plus anciens
I am trying to interpolate observation heights from pressures in 3D space. I am using interp3 because it is generally faster than griddata, however I return this error, and I am not really sure what NDGRID structure means. I am running it in a loop as well. The following sizes of variables in interp3(X,Y,Z,V,Xq,Yq,Zq):
for ns=1:length(obs_is)
obs_hts(ns)=interp3(repmat(y_pts,[1 1 size(h,3)]),repmat(x_pts,[1 1 size(h,3)]),double(p),double(h),obs_lats(ns),obs_lons(ns),obs_pres(ns));
end
X: latitudes in 3D model space (199x199x50)
Y: longitudes in 3D model space (199x199x50)
Z: pressure in 3D model space (199x199x50)
V: heights in 3D model space (199x199x50)
Xq: observation latitude (scalar)
Vq: observation longitude (scalar)
Zq: observation pressure (scalar)
And yes, these are all double (not single) variables.
Réponses (2)
Star Strider
le 27 Août 2023
0 votes
10 commentaires
KCE
le 27 Août 2023
So you have a grid with 199 coordinates in x-direction, 199 coordinates in y-direction and 50 coordinates in z direction that are monotonically increasing, you then use ndgrid to build X, Y and Z and then call interpn(X,Y,Z,V,Xq,Yq,Zq) as required ? I don't get the impression that you follow this path in your code.
KCE
le 28 Août 2023
I don't. If you had called "ndgrid" before, a call to "repmat" were not necessary.
Follow the steps described, and the interpolation will work:
x = -3:3;
y = -1:1;
z = -10:10;
[X,Y,Z] = ndgrid(x,y,z);
V = X.^2 + Y.^3 + Z.^4;
interpn(X,Y,Z,V,0.1,0.5,-2)
KCE
le 28 Août 2023
Star Strider
le 28 Août 2023
The matrices you are using must already be in ndgrid format. You need to check those.
Check to see that the matrices Thorsten’s ndgrid call produces, or create your own to check them, for example:
x = 1:3;
y = 4:6;
z = 7:9;
[X,Y,Z] = ndgrid(x,y,z)
to see if yours match that formst.
The interpolation should then work.
.
Walter Roberson
le 28 Août 2023
The values in the x-array are strictly monotonic, increasing, and vary along the second dimension. The values in the y-array are strictly monotonic, increasing, and vary along the first dimension. The values in the z-array are strictly monotonic, increasing, and vary along the third dimension. Use the meshgrid function to create a full grid that you can pass to interp3.
The documentation never states anywhere here (or in griddedInterpolant) that the arrays must vary only along the specified dimensions, but in my tests that turns out to be required.
So if you have, for example, set of geographic coordinates that are in degrees but are sampled in equal physical differences, then the degree coordinates would be rectilinear, and could easily obey the rules about being sorted and monotonically increasing along the specified dimension. None the less, interp3() and griddedInterpolant() will reject such coordinates, demanding regular cuboid coordinates.
If your coordinates are not in regular cuboids, then you should convert them to vectors and use scatteredInterpolant()
Paul
le 28 Août 2023
Shouldn’t interp3 catch the problem with the inputs before it passes them on to griddedInterpolant?
I can envision someone seeing the error message, sending ndgrid format into interp3 and then being confused when another error message is shown (or worse, getting an incorrect result; I can’t test this at present)
Walter Roberson
le 28 Août 2023
interp3() has a try/catch that tests the error generated by griddedInterpolant() and conditionally substitutes a different error message referencing meshgrid(). But perhaps the error codes got updated while the interp3() error code test did not get updated?
That sounds like an odd way to handle input argument checking to interp3. Seems like that should be done at the interp3 level before the call to griddedInterpolant.
At least calling interp3 with ndgrid inputs generates the expected error
[X,Y,Z] = ndgrid(1:4);
V = X + Y + Z;
interp3(X,Y,Z,V,1.5,2.5,3.5)
Bruno Luong
le 28 Août 2023
Modifié(e) : Bruno Luong
le 29 Août 2023
Check with this
for ns=1:length(obs_is)
X = repmat(y_pts,[1 1 size(h,3)]);
Y = repmat(x_pts,[1 1 size(h,3)]);
Z = double(p);
if ~all(X == X(1,:,1), 'all')
if all(X == X(:,1,1), 'all') && all(Y == Y(1,:,1), 'all')
warning('You might swap X and Y')
else
error('X for ns=%d X is not meshgrid generated', ns);
end
end
if ~all(Y == Y(:,1,1), 'all')
error('Y for ns=%d Y is not meshgrid generated', ns);
end
if ~all(Z == Z(1,1,:), 'all')
error('Z for ns=%d Z is not meshgrid generated', ns);
end
end
It allows to see further why your data is incorrectly gridded for interp3
Catégories
En savoir plus sur Stair Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!