Matlab: repeat every column sequentially n times [duplicate]

Suppose you have this simplified input and you want to expand columns sequentially n times:

A   = [1 4
       2 5
       3 6];

szA = size(A); 
n = 3;

There are few ways to do that:

  • Replicate, then reshape:

    reshape(repmat(A,n,1),szA(1),n*szA(2))
    
  • Kronecker product:

    kron(A,ones(1,n))
    
  • Using FEX: expand():

    expand(A,[1 n])
    
  • Since R2015a, repelem():

    repelem(A,1,n)
    

All yield the same result:

ans =
     1     1     1     4     4     4
     2     2     2     5     5     5
     3     3     3     6     6     6

Leave a Comment