假设我有矩阵
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矩阵?
答案 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])