Practical differences between meshgrid and ndgrid?

hhh*_*hhh 4 matlab

This discussion is getting dense here about pageIdx with ndgrid. I can understand now meshgrid but not ndgrid -- please elaborate with practical examples where it could be used. It is not really the same as meshgrid: some odd inversions in values so where it is meant to be used?

Jon*_*nas 5

假设您想知道数组的行/列/ etc索引.您可以使用meshgrid它,但它将首先输出列,然后输出行索引,这可能是错误的来源.

而不是meshgrid,你可以使用ndgrid,这将给出预期顺序的指数,即

[rowIndices, colIndices] = ndgrid(1:size(array,1),1:size(array,2));
Run Code Online (Sandbox Code Playgroud)

或者,对于3D

[rowIndices, colIndices, pageIndices] = ndgrid(1:size(array,1),1:size(array,2),1:size(array,3));
Run Code Online (Sandbox Code Playgroud)

meshgrid在处理绘图时,或者使用图像处理工具箱函数的某些输出时,前两个索引的反转可能会有所帮助.

但是,当您想要计算4D高斯的值时,您唯一的选择是使用ndgrid:

[xx,yy,zz,tt] = ndgrid(-3:0.1:3);
out = exp(-xx.^2-yy.^2-zz.^2-tt.^2);
Run Code Online (Sandbox Code Playgroud)