Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: repeat every column sequentially n times [duplicate]

I'm pretty much beginner so it's probably possible to do what I want in a simple way. I have a matrix 121x62 but I need to expand it to 121x1488 so every column has to be repeated 24 times. For example, transform this:

   2.2668       2.2667       2.2667       2.2666       2.2666       2.2666       
   2.2582       2.2582       2.2582       2.2582       2.2581       2.2581       
    2.283        2.283        2.283       2.2829       2.2829       2.2829       
   2.2881       2.2881       2.2881       2.2881       2.2881        2.288        
    2.268        2.268       2.2679       2.2679       2.2678       2.2678       
   2.2742       2.2742       2.2741       2.2741       2.2741        2.274    

into this:

2.2668     2.2668     2.2668  and so on to 24th     2.2667     2.2667  and again to 24x
2.2582     2.2582     2.2582 ...

with every column.

I've tried to create a vector with these values and then transform with vec2mat and ok I have 121x1488 matrix but repeated by rows:

2.2668   2.2668   2.2668  2.2668  2.2668  2.2668 ...    2.2582   2.2582  2.2582  2.2582 ...

How to do it by columns?

like image 377
papkin Avatar asked Apr 28 '13 19:04

papkin


2 Answers

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
like image 62
Oleg Avatar answered Oct 14 '22 19:10

Oleg


Just for the sake of completeness. If you want to duplicate along the rows, you can also use rectpulse().

A = [1,2,3;...
     4,5,6];

n = 3;

rectpulse(A, n);

gives you

1  2  3
1  2  3
1  2  3
4  5  6
4  5  6
4  5  6
like image 37
Malte Avatar answered Oct 14 '22 18:10

Malte