1

I have a matrix and I want to duplicate each row n times, such that each row is consecutively stacked n times.

So basically if

n = 2

then my matrix

A = [1 2 3; 4 5 6; 7 8 9]

should become

B = [1 2 3; 1 2 3; 4 5 6; 4 5 6; 7 8 9; 7 8 9] .

Thanks in advance.

Raynor
  • 267
  • 3
  • 10

1 Answers1

9

This question has been asked quite a few times before, for instance here, here and here (from today) .

Some solutions:

kron(A,ones(n,1))
ans =

     1     2     3
     1     2     3
     4     5     6
     4     5     6
     7     8     9
     7     8     9

Another one:

reshape(repmat(A(:)',n,[]),[],3);

And one more:

B = A(ceil((1:size(A,1)*n)/n),:)

Take your pick!

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70