【问题标题】:Create 3D matrix from MULTIPLES 1xm matrix从 MULTIPLES 1xm 矩阵创建 3D 矩阵
【发布时间】:2017-05-25 05:50:54
【问题描述】:

是否可以从 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)];

任何有效的方法来做到这一点? 感谢您的任何意见!

【问题讨论】:

    标签: matlab matrix 3d


    【解决方案1】:

    您可以使用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之后permuteK

    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]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多