【问题标题】:Create a 3D array made of repetitions of a matrix in a specific way以特定方式创建由矩阵重复组成的 3D 数组
【发布时间】:2019-09-18 15:49:17
【问题描述】:

我想以我无法使用函数 repmat 的方式重复矩阵。也就是说,我想要一个矩阵 B,它的按列的横截面会给我矩阵 A。当我使用 repmat(A,[1,1,col_nb]) 时,你必须取第三维的横截面才能得到矩阵 A .

下面我举个例子:

A = [1,2,3;4,5,6];
not_B = repmat(A,[1,1,3]); % What I am getting at the moment

% not_B(:,:,1) = A
% not_B(:,:,2) = A
% not_B(:,:,3) = A

% What I would like
B(:,1,:) = A;
B(:,2,:) = A;
B(:,3,:) = A;

【问题讨论】:

    标签: arrays matlab multidimensional-array


    【解决方案1】:

    你可以使用repelem来避免额外的permute操作:

    [r c] = size(A);
    rep = 3;
    B = reshape(repelem(A,1,rep),r,rep,c)
    

    【讨论】:

      【解决方案2】:

      我相信您只需致电permute

      B = permute(repmat(A, [1 1 3]), [1 3 2]);
      

      但是,应该注意的是,您从 B(:, i, :) 获得的矩阵将不再是 3×3。它们将是 3×1×3。您可能想像这样使用squeeze

      C = squeeze(B(:, 1, :));  % C will match A
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-09
        • 1970-01-01
        • 2022-01-24
        相关资源
        最近更新 更多