类似于Meshgrid的函数在Matlab中生成4D数组 [英] Meshgrid-like function to generate 4D arrays in Matlab

查看:178
本文介绍了类似于Meshgrid的函数在Matlab中生成4D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在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.meshgridndgrid的特例.

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.

但是,我要提到的一个警告是,XY坐标在meshgridndgrid之间的排列方式是互换的.具体来说,Xmeshgrid中水平移动,而Xndgrid中垂直移动.类似地,Ymeshgrid中垂直遍历,而Yndgrid中水平遍历.

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.每个函数输出的坐标相同,但是前两个维度中输出数组的形状不同.对于长度分别为MNP的网格矢量x1gv, x2gvx3gvndgrid(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 to meshgrid, however ndgrid supports 1-D to N-D while meshgrid 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 vectors x1gv, x2gv and x3gv of length M, N and P respectively, ndgrid(x1gv, x2gv) will output arrays of size M-by-N while meshgrid(x1gv, x2gv) outputs arrays of size N-by-M. Similarly, ndgrid(x1gv, x2gv, x3gv) will output arrays of size M-by-N-by-P while meshgrid(x1gv, x2gv, x3gv) outputs arrays of size N-by-M-by-P.

这篇关于类似于Meshgrid的函数在Matlab中生成4D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆