Repeat each row of matlab matrix

Alec Jacobson

February 16, 2016

weblog/

I swear I've posted this before, but here's how to turn:

X = [11 12 13; ...
     21 22 23];

Into:

Y = [
  11 12 13; ...
  11 12 13; ...
  11 12 13; ...
  11 12 13; ...
  21 22 23; ...
  21 22 23; ...
  21 22 23; ...
  21 22 23];

That is, repeat each row, but maintain the order

Y = reshape(permute(repmat(X,[1 1 num_reps]),[3 1 2]),[],size(X,2));