Element-wise array replication in Matlab

2019-01-01 02:50发布

问题:

Let\'s say I have a one-dimensional array:

a = [1, 2, 3];

Is there a built-in Matlab function that takes an array and an integer n and replicates each element of the array n times?

For example calling replicate(a, 3) should return [1,1,1,2,2,2,3,3,3].

Note that this is not at all the same as repmat. I can certainly implement replicate by doing repmat on each element and concatenating the result, but I am wondering if there is a built in function that is more efficient.

回答1:

As of R2015a, there is a built-in and documented function to do this, repelem:

repelem Replicate elements of an array.
    W = repelem(V,N), with vector V and scalar N, creates a vector W where each element of V is repeated N times.

The second argument can also be a vector of the same length as V to specify the number of replications for each element. For 2D replication:

B = repelem(A,N1,N2)

No need for kron or other tricks anymore!

UPDATE: For a performance comparison with other speedy methods, please see the Q&A Repeat copies of array elements: Run-length decoding in MATLAB.



回答2:

I\'m a fan of the KRON function:

>> a = 1:3;
>> N = 3;
>> b = kron(a,ones(1,N))

b =

    1     1     1     2     2     2     3     3     3

You can also look at this related question (which dealt with replicating elements of 2-D matrices) to see some of the other solutions involving matrix indexing. Here\'s one such solution (inspired by Edric\'s answer):

>> b = a(ceil((1:N*numel(a))/N))

b =

    1     1     1     2     2     2     3     3     3


回答3:

a = [1 2 3];
N = 3;

b = reshape(repmat(a,N,1), 1, [])


回答4:

>> n=3;
>> a(floor((0:size(a,2)*n-1)/n)+1)

ans =

     1     1     1     2     2     2     3     3     3


回答5:

Some exotic alternatives. Admittedly more funny than useful:

  1. Assign the (first) result of meshgrid to a vector:

    b = NaN(1,numel(a)*n); %// pre-shape result
    b(:) = meshgrid(a,1:n);
    
  2. Build a matrix that multiplied by a gives the result:

    b = a * fliplr(sortrows(repmat(eye(numel(a)),n,1))).\';
    
  3. Use ind2sub to generate the indices:

    [~, ind] = ind2sub([n 1],1:numel(a)*n);
    b = a(ind);
    


回答6:

If you have the image processing toolbox, there is another alternative:

N = 3;
imresize(a, [1 N*numel(a)],\'nearest\')


回答7:

% To get b = [1 1 1 2 2 2 3 3 3]
N = 3;
a = [1 2 3];
temp_a = a(ones(N,1),:);
b = reshape(temp_a,1,numel(temp_a));

% To get b = [1 2 3 1 2 3 1 2 3]
N = 3;
a = [1 2 3];
temp_a = a(ones(N,1),:);
b = reshape(temp_a\',1,numel(temp_a));