从MULTIPLES 1xm矩阵创建3D矩阵

时间:2017-05-25 05:50:54

标签: matlab matrix 3d

是否可以从1xm矩阵创建3D矩阵?我想将矩阵的每一列分成第三维。这是related question。但是,它并没有涉及1xm矩阵的倍数,我做了。

a=randi([1 5], [1 100]);
b=randi([1 5], [1 100]);
c=randi([1 5], [1 100]);
d=randi([1 5], [1 100]);

%this is where I wanted to split the matrix when assembling
K=[b d a;
    b 0 a;
    b c a];

结果K矩阵应为3x3x100。在每个第三维中,a b c d的值是它们对应的列。例如:

k(:,:,1)=[b(1,1) d(1,1) a(1,1);
b(1,1) 0 a(1,1);
b(1,1) c(1,1) a(1,1)];
.
.
.
k(:,:,n)=[b(1,n) d(1,n) a(1,n);
b(1,n) 0 a(1,n);
b(1,n) c(1,n) a(1,n)];

有效的方法吗? 感谢您的任何意见!

1 个答案:

答案 0 :(得分:2)

您可以使用permute来交换矢量尺寸:

a=randi([1 5], [1 100]);
b=randi([1 5], [1 100]);
c=randi([1 5], [1 100]);
d=randi([1 5], [1 100]);
% permute dimensions to 1x1x100
a = permute(a,[1 3 2]);
b = permute(b,[1 3 2]);
c = permute(c,[1 3 2]);
d = permute(d,[1 3 2]);
%this is where I wanted to split the matrix when assembling
K=[b,d,a;
    b,zeros(size(a)),a;
    b,c,a];
size(K) % 1x1x100

在concat之后你也可以permute K

a=randi([1 5], [1 100]);
b=randi([1 5], [1 100]);
c=randi([1 5], [1 100]);
d=randi([1 5], [1 100]);
K = cat(3,[b;b;b],[d;zeros(size(a));c],[a;a;a]);
K = permute(K,[1 3 2]);
相关问题