Matlab: repeat each column sequentially n times

I start a lot, so it's possible to do what I want in a simple way. I have a 121x62 matrix, but I need to expand it to 121x1488 so that each column repeats 24 times. For example, convert 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 

in it:

 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 each column.

I tried to create a vector with these values, and then convert using vec2mat and ok. I have a 121x1488 matrix, but it repeats with the lines:

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

How to do it column by column?

+7
source share
3 answers

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

 A = [1 4 2 5 3 6]; szA = size(A); n = 3; 

There are several ways to do this:

  • Replicate, then change the form:

     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 give the same result:

 ans = 1 1 1 4 4 4 2 2 2 5 5 5 3 3 3 6 6 6 
+22
source

Just for the sake of completeness. If you want to duplicate along lines, 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 
+1
source

Here you go:

 function [result] = repcolumn(A, n) %n - how many times each column from A should be repeated [rows columns] = size(A); result = repmat(A(:,1),1,n); for i = 2:columns result = [result,repmat(A(:,i),1,n)]; end end 

There should be an easier way, but it does the job.

0
source

All Articles