且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-10 10:13:01

您需要命令

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.