将3D矩阵重塑为2D矩阵

将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(:,:)压缩最后两个维度,但重塑速度更快.

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.