MATLAB: Repeating blocks of N rows M times

matrix manipulationrepeat rowsrepmat

Hello,
Say I have a matrix like:
A= [1,2,3; 4,5,6; 7,8,9; 10,11,12];
I would like to copy chunks of N rows M times from this to create a new matrix, for example if N=2 and M=2 I would get:
A= [1,2,3; 4,5,6; 1,2,3; 4,5,6; 7,8,9; 10,11,12; 7,8,9; 10,11,12];
How can I do this in an efficient way?
Regards

Best Answer

A = reshape(1:18,3.,[])';
N=2;
M=2;
[~,n] = size(A);
A2 = reshape(kron(reshape(A',n*N,[]),ones(1,M)),n,[])';