我正在尝试以行方式将矢量转换为3d矩阵。
例如,我的矢量是:
a = 1:18;
我希望将其转换为2x3x3矩阵:
b(:,:,1) = [1 2 3; 4 5 6];
b(:,:,2) = [7 8 9; 10 11 12];
b(:,:,3) = [13 14 15; 16 17 18];
但重塑函数(即reshape(a, 2,3,3)
)以列式方式排列元素:
val(:,:,1) =
1 3 5
2 4 6
val(:,:,2) =
7 9 11
8 10 12
val(:,:,3) =
13 15 17
14 16 18
如何从向量a?
获得矩阵b答案 0 :(得分:1)
b = permute(reshape(a,3,2,3),[2 1 3]);
b(:,:,1) =
1 2 3
4 5 6
b(:,:,2) =
7 8 9
10 11 12
b(:,:,3) =
13 14 15
16 17 18
答案 1 :(得分:0)
我找到了一个(或者)解决方案:
b = permute(reshape(reshape(a,3,[])',2,3,[]), [1,3,2])
b(:,:,1) =
1 2 3
4 5 6
b(:,:,2) =
7 8 9
10 11 12
b(:,:,3) =
13 14 15
16 17 18