类似于Meshgrid的函数在Matlab中生成4D数组 [英] Meshgrid-like function to generate 4D arrays in Matlab
问题描述
我有一个要在matlab中实现的4维数学函数,但meshgrid
函数最多只能在3维上使用.有更多尺寸的类似功能吗? (我不想用于循环).
I have a 4 dimension mathematical function I want to implement in matlab, but the meshgrid
function works for at most 3 dimensions. Is there a similar function for more dimensions? (I don't want to use for loops).
推荐答案
是的,请使用 ndgrid
. ndgrid
的尺寸超出3,但您可以肯定将其用于2或3.meshgrid
是ndgrid
的特例.
Yes, use ndgrid
. ndgrid
extends for dimensions beyond 3, but you can most certainly use it for 2 or 3. meshgrid
is a special case of ndgrid
.
但是,我要提到的一个警告是,X
和Y
坐标在meshgrid
和ndgrid
之间的排列方式是互换的.具体来说,X
在meshgrid
中水平移动,而X
在ndgrid
中垂直移动.类似地,Y
在meshgrid
中垂直遍历,而Y
在ndgrid
中水平遍历.
However, one caveat I would like to mention is that the way the X
and Y
coordinates are arranged between meshgrid
and ndgrid
are interchanged. Specifically, X
traverses horizontally in meshgrid
while X
traverses vertically in ndgrid
. Similarly Y
traverses vertically in meshgrid
while Y
traverses horizontally in ndgrid
.
因此,要在2个或3个维度上获得相同的行为,请执行以下操作:
Therefore, to get the same behaviour for 2 or 3 dimensions, do:
[X,Y] = meshgrid(...)
[Y,X] = ndgrid(...)
或者:
[X,Y,Z] = meshgrid(...)
[Y,X,Z] = ndgrid(...)
对于较大的尺寸,使用ndgrid
可以在水平和垂直坐标方面保持相同的顺序.
Using ndgrid
for higher dimensions maintains that same order with regards to the horizontal and vertical coordinates.
更明确地说,这是ndgrid
的注释在文档末尾所说的:
To be more explicit, this is what the note for ndgrid
says at the end of the documentation:
ndgrid
功能类似于meshgrid
,但是ndgrid
支持1-D到N-D,而meshgrid
限于2-D和3-D.每个函数输出的坐标相同,但是前两个维度中输出数组的形状不同.对于长度分别为M
,N
和P
的网格矢量x1gv, x2gv
和x3gv
,ndgrid(x1gv, x2gv)
将输出大小为M
-by-c26>的数组,而meshgrid(x1gv, x2gv)
将输出大小为M
的数组N
-M
.类似地,ndgrid(x1gv, x2gv, x3gv)
将输出大小为M
-<-c26> -by-P
的数组,而meshgrid(x1gv, x2gv, x3gv)
输出大小为N
-M
-by-P
的数组. /p>
The
ndgrid
function is similar tomeshgrid
, howeverndgrid
supports 1-D to N-D whilemeshgrid
is restricted to 2-D and 3-D. The coordinates output by each function are the same, but the shape of the output arrays in the first two dimensions are different. For grid vectorsx1gv, x2gv
andx3gv
of lengthM
,N
andP
respectively,ndgrid(x1gv, x2gv)
will output arrays of sizeM
-by-N
whilemeshgrid(x1gv, x2gv)
outputs arrays of sizeN
-by-M
. Similarly,ndgrid(x1gv, x2gv, x3gv)
will output arrays of sizeM
-by-N
-by-P
whilemeshgrid(x1gv, x2gv, x3gv)
outputs arrays of sizeN
-by-M
-by-P
.
这篇关于类似于Meshgrid的函数在Matlab中生成4D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!