Element-wise array replication in Matlab
Asked Answered
M

7

18

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.

Mesenchyme answered 22/12, 2009 at 17:23 Comment(0)
S
14

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.

Sassoon answered 10/12, 2014 at 23:18 Comment(2)
How did you know this before R2015a was out?!Negativism
@LuisMendo Pre-release. I love reading release notes almost as much as accumarray. :). But I immediately deleted my answer an then undeleted it on release day so as not to break the terms of the pre-release.Sassoon
S
21

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
Sabu answered 22/12, 2009 at 17:24 Comment(2)
what a nice solution, but what about if N be a matrix of frequency of elements of a, means that each element of N is correspond with one element of a?Alunite
@ghedas: That's addressed here, if you hadn't yet seen it.Sabu
P
20
a = [1 2 3];
N = 3;

b = reshape(repmat(a,N,1), 1, [])
Positivism answered 22/12, 2009 at 19:28 Comment(1)
unlike kron, this solution is purely memory management, and does not drag multiplication into it.Burrell
S
14

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.

Sassoon answered 10/12, 2014 at 23:18 Comment(2)
How did you know this before R2015a was out?!Negativism
@LuisMendo Pre-release. I love reading release notes almost as much as accumarray. :). But I immediately deleted my answer an then undeleted it on release day so as not to break the terms of the pre-release.Sassoon
J
3
>> n=3;
>> a(floor((0:size(a,2)*n-1)/n)+1)

ans =

     1     1     1     2     2     2     3     3     3
Jyoti answered 27/9, 2012 at 9:55 Comment(0)
N
3

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);
    
Negativism answered 27/12, 2013 at 22:33 Comment(0)
C
2

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

N = 3;
imresize(a, [1 N*numel(a)],'nearest')
Circumvent answered 25/1, 2012 at 13:33 Comment(1)
Actually, your code is wrong. It should be imresize(a, [1 N*numel(a)],'nearest'), with an N, not a 2. When you correct this, your solution is actually about 3 times slower than the KRON solution (and 5 times slower than the indexing solution a(ceil((1:N*numel(a))/N))).Sabu
M
1
% 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));
Maximalist answered 13/8, 2012 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.