将 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
)
我该怎么做?
您需要命令 重塑
:
You need the command reshape
:
假设你的初始矩阵是(只是为了让我得到一些数据):
Say your initial matrix is (just for me to get some data):
a=rand(4,6,8);
然后,如果最后两个坐标是空间坐标(时间为 4,m 为 6,n 为 8),则使用:
Then, if the last two coordinates are spatial (time is 4, m is 6, n is 8) you use:
a=reshape(a,[4 48]);
你最终会得到一个 4x48 的数组.
and you end up with a 4x48 array.
如果前两个是空间的,最后一个是时间(m 是 4,n 是 6,时间是 8)你使用:
If the first two are spatial and the last is time (m is 4, n is 6, time is 8) you use:
a=reshape(a,[24 8]);
你最终会得到一个 24x8 的数组.
and you end up with a 24x8 array.
这是一个快速的 O(1) 操作(它只是调整数据形状的标题).还有其他方法可以做到这一点,例如a=a(:,:)
压缩最后两个维度,但 reshape 更快.
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.