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.

0

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. My apologies for the duplicate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.