将2 x 4矩阵更改为2 x 2 2 4矩阵

时间:2019-10-17 12:35:09

标签: matlab matrix

假设我有矩阵

A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];

我将生成大小为B的矩阵[2,2,4]

B(:,:,1) = [1 2; 5 6];
B(:,:,2) = [9 10; 13 14];
B(:,:,3) = [3 4; 7 8];
B(:,:,4) = [11 12; 15 16];

如何生成此3D矩阵?

1 个答案:

答案 0 :(得分:2)

使用mat2cell的一种解决方案:

dim1 = [2,2];                 %first dimension of the submatrix
dim2 = [2,2];                 %second dimension of the submatrix
C = mat2cell(A,dim1,dim2)     %for square submatrix C=mat2cell(A,dim1) work also
B = reshape([C{:}],[2,2,4]) 
相关问题