Why such a fuss with ndgrid and meshgrid issues?
13 次查看(过去 30 天)
显示 更早的评论
Folks,
In the world of artificial intelligence and more intelligent programming, I would like to see more intelligence put into place with how function like griddata, imagesc, pcolor, and surf interpret their inputs. There is an unfortunate degree of confusion that has persisted since the early 2000's at least with how to interpret arrays in terms of Cartesian axes. I am troubled today with trying to understand how griddata is working on input that I setup using ndgrid. I appear to have something wrong, and earth layers are not dipping in the direction that I expect. The documentation says griddata can work with either meshgrid or griddata input, which makes my head spin given my understanding of both meshgrid and ndgrid. What is really troubling is that the Mathworks documentation routinely contains only simple examples where the person that created the documentation made "quicky" non-realistic examples where they simply created input arrays that had exactly the same x and y dimensions, which does not help anyone in my situation who is dealing with realistic inputs that do not have the same dimension lengths. I request that future documentation efforts spend a little extra time making non-symmetric input x and y arrays as examples.
5 个评论
Voss
2024-7-5
Don't use reshape for that; use permute:
% Using meshgrid approach for non-square slice example
x = -2:0.2:2; y = -5:0.1:5; z = -25:2:25;
[X,Y,Z] = meshgrid(x,y,z);
V = X.*exp(-X.^2-Y.^2-Z.^2);
xslice = [0.8]; yslice = []; zslice = [];
figure; slice(X,Y,Z,V,xslice,yslice,zslice)
% Using ndgrid approach for non-square slice example
x = -2:0.2:2; y = -5:0.1:5; z = -25:2:25;
[X,Y,Z] = ndgrid(x,y,z);
nX = length(x); nY = length(y); nZ = length(z);
xg = permute(X,[2,1,3]);
yg = permute(Y,[2,1,3]);
zg = permute(Z,[2,1,3]);
V = xg.*exp(-xg.^2-yg.^2-zg.^2);
xslice = [0.8]; yslice = []; zslice = [];
figure; slice(xg,yg,zg,V,xslice,yslice,zslice)
采纳的回答
更多回答(4 个)
Paul
2024-7-5
编辑:Paul
2024-7-6
Hi Kristoffer,
The inputs to griddata that can be in either meshgrid or ndgrid format are the query points in Xq and Yq. I think that their specific format doesn't matter because griddata processes each pair ( Xq(i,j),Yq(i,j) ) where the output vq(i,j) = f(Xq(i,j) , Yq(i,j)) is the interpolation based on Delaunay triangulation of scattered sample points and sample values (in contrast to, for example, interp2 that requires the sample points and values to be in meshgrid format).
What would matter is what's done with Xq, Yq, and vq after the call to gridddata. For example, surf prefers (requires?) the input data to be in meshgrid format, so a call to griddata should use query points in meshgrid format if to be followed by a call to surf with the results.
If having a problem with griddata with query points in ndgrid format, you're likely to get more help by posting a simple code example to show what the issue is.
Edit: As shown by @Star Strider in this Answer, surf is agnostic to whether or not the the input is in ndgrid/meshgrid format.
Edit: Use of surf with meshgrid/ndgrid was also explained by @Voss in this thread that I had forgotten about.
I think my confusion with surf (et. al.?) might be the result of a long-ago behavior change that still hasn't solidified with me.
In surf (2019a), the doc stated "To create a matrix for arbitrary domains, use the meshgrid function." No ambiguity there.
In surf (2019b) the doc states: "You can use the meshgrid function to create X and Y matrices." ("can" not "must") which suggests something changed from 2019a to 2019b, though there is no mention of such in the 2019b release notes.
David Goodmanson
2024-7-5
编辑:David Goodmanson
2024-7-5
Hi Kristoffer,
I totally agree that Mathworks should use nonsquare examples in the documentation for meshgrid, ndgrid, surf etc. It just increases awareness. And comparing [x y] = meshgrid(a,b) and [x y] = ndhgrid(a,b),
***** the meshgrid x matrix and ndgrid x matrix are transposes of each other. Same for y.
I think you meant to say that griddata works with either meshgrid or ndgrid input, and that's true. Griddata is agnostic on the topic, assuming of course that either meshgrid or ndgrid are used consistently throughout.
Star Strider
2024-7-5
I’m not certain what the problem is.
The choce of meshgrid or ndgrid does not seem to be an issue, at least in this example —
xv = linspace(-2, 2, 15);
yv = linspace(-1, 4, 19);
[X1,Y1] = meshgrid(xv, yv);
Z1 = exp(-X1.^2) .* exp(-(Y1-2.5).^2*1.5)*2.5;
figure
surf(X1, Y1, Z1)
colormap(turbo)
axis('equal')
title('Original')
F = scatteredInterpolant(X1(:), Y1(:), Z1(:));
xv2 = linspace(-2, 2, 40);
yv2 = linspace(-1, 4, 30);
[X2,Y2] = ndgrid(xv2,yv2);
Z2 = F(X2,Y2);
figure
surf(X2,Y2,Z2)
colormap(turbo)
axis('equal')
title('scatteredInterpolant — ndgrid')
[X2,Y2] = meshgrid(xv2,yv2);
Z2 = F(X2,Y2);
figure
surf(X2,Y2,Z2)
colormap(turbo)
axis('equal')
title('scatteredInterpolant — meshgrid')
Do either of these deal with the issue you’re describing?
.
4 个评论
William Rose
2024-7-5
@Kristoffer Walker, I definitely agree with you. I use inputs that are non-square when i use surf() and related routines. Thus I use vectors X, Y with different lengths in surf(X,Y,Z), etc. If the inputs are square, I may flip things around by mistake, and I might not know it, because there's no error message.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!