将 3d 矩阵重塑为 2d 矩阵
我在 MATLAB 中有一个 3d 矩阵 (n-by-m-by-t
),表示一段时间内网格中的 n-by-m
测量值。我想要一个二维矩阵,其中空间信息消失了,仅留下 n*m
随着时间 t
的测量(即:n*m- by-t
)
我该怎么做?
I have a 3d matrix (n-by-m-by-t
) in MATLAB representing n-by-m
measurements in a grid over a period of time. I would like to have a 2d matrix, where the spatial information is gone and only n*m
measurements over time t
are left (ie: n*m-by-t
)
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要命令
reshape
:说你的初始矩阵是(只是为了让我获取一些数据):
然后,如果最后两个坐标是空间坐标(时间是4,m是6,n是8),你使用:
并且最终得到一个4x48数组。
如果前两个是空间的,最后一个是时间(m 为 4,n 为 6,时间为 8),则使用:
最终得到一个 24x8 数组。
这是一个快速的 O(1) 操作(它只是调整数据形状的标头)。还有其他方法可以做到这一点,例如
a=a(:,:)
来压缩最后两个维度,但重塑速度更快。You need the command
reshape
:Say your initial matrix is (just for me to get some data):
Then, if the last two coordinates are spatial (time is 4, m is 6, n is 8) you use:
and you end up with a 4x48 array.
If the first two are spatial and the last is time (m is 4, n is 6, time is 8) you use:
and you end up with a 24x8 array.
This is a fast, O(1) operation (it just adjusts it header of what the shape of the data is). There are other ways of doing it, e.g.
a=a(:,:)
to condense the last two dimensions, but reshape is faster.Reshape 当然是重塑数组的标准解决方案。 (他们还会叫它什么?)还有一些技巧有待揭开。
首先,将大小为 [n,m,p] 的数组转换为大小为 [n*m,p] 的数组最简单的方法是什么?
但更好的是:
如果您将 reshape 的参数之一留空,则 reshape 会为您计算大小!小心,如果你尝试这样做并且 A 的大小不符合,那么你会得到一个错误。因此:从
逻辑上讲,我们不能从包含 9 个元素的数组中创建一个包含两列的数组。我确实在 MATLAB Central 交换上放置了一个名为 wreshape 的函数,该函数将根据需要进行填充执行此操作不会产生错误。
当然,您始终可以使用一些技巧,例如
直接从矩阵创建向量。这相当于形式
Reshape is of course the standard solution to reshaping an array. (What else would they call it?) There are still a few tricks to uncover.
First of all, the simplest way to turn an array of size [n,m,p] into an array of size [n*m,p]?
But better is this:
If you leave one of the arguments to reshape empty, then reshape computes the size for you! Be careful, if you try this and the size of A does not conform, then you will get an error. Thus:
Logically, we cannot create an array of with two columns from something that has 9 elements in it. I did put a function called wreshape on the MATLAB Central exchange that would pad as necessary to do this operation with no error generated.
Of course, you can always use tricks like
to create a vector directly from a matrix. This is equivalent to the form
解决此问题的更好方法是使用 Rasmus Bro 的 N-Way Toolbox 中提供的 nshape.m。这将允许您沿着给定模式重塑,这对于重塑命令来说有点棘手。
该工具箱可通过以下链接获取:
https://www. mathworks.com/matlabcentral/fileexchange/1088-the-n-way-toolbox
还有一些其他优秀的工具可以帮助进行数组操作。
A better solution to this problem is to use nshape.m available from Rasmus Bro's N-Way Toolbox. That will allow you to reshape along a given mode, which is a little more tricky with the reshape command.
The toolbox is available at the following link:
https://www.mathworks.com/matlabcentral/fileexchange/1088-the-n-way-toolbox
There are some other excellent tools there to help with array manipulation as well.