Element-wise Matrix Replication in MATLAB

前端 未结 1 1576
失恋的感觉
失恋的感觉 2021-01-05 06:30

I have a 3 dimensional matrix. I want to replicate the matrix of size 8x2x9 to a specified number of times in the third dimension given by a vector say [3, 2, 1, 1, 5,

相关标签:
1条回答
  • 2021-01-05 06:40

    For R2015a and newer...

    According to the documentation for repelem (first introduced in version R2015a), it can operate on matrices as well. I believe the following code should accomplish what you want (I can't test it because I have an older version):

    newMat = repelem(mat, 1, 1, [3 2 1 1 5 4 2 2 1]);
    

    For pre-R2015a versions...

    You can use one of the approaches from this question to replicate an index into the third dimension, then simply index your matrix with that. For example (adapting Divakar's solution):

    vals = 1:size(mat, 3);
    clens = cumsum([3 2 1 1 5 4 2 2 1]);
    index = zeros(1, clens(end));
    index([1 clens(1:end-1)+1]) = diff([0 vals]);
    newMat = mat(:, :, cumsum(index));
    

    You can then generalize this into a function to operate on multiple dimensions like repelem does:

    function A = my_repelem(A, varargin)
    
      index = cell(1, nargin-1);
      for iDim = 1:nargin-1
        lens = varargin{iDim};
        if isscalar(lens)
          if (lens == 1)
            index{iDim} = ':';
            continue
          else
            lens = repmat(lens, 1, size(A, iDim));
          end
        end
        vals = 1:size(A, iDim);
        clens = cumsum(lens);
        index{iDim} = zeros(1, clens(end));
        index{iDim}([1 clens(1:end-1)+1]) = diff([0 vals]);
        index{iDim} = cumsum(index{iDim});
      end
      A = A(index{:});
    
    end
    

    And for your sample data, you would use it like so:

    >> A(:,:,1) = [1 2; 1 2];
    >> A(:,:,2) = [2 3; 2 3];
    >> A = my_repelem(A, 1, 1, [1 2])
    
    A(:,:,1) =
    
         1     2
         1     2
    
    A(:,:,2) =
    
         2     3
         2     3
    
    A(:,:,3) =
    
         2     3
         2     3
    
    0 讨论(0)
提交回复
热议问题