MATLAB: How to repeat rows of matrix

insertmatrixrows

Hi,
I have a matrix and a vector indicating how many times the corresponding lines in the matrix should appear. I'd like to turn this into a single matrix.
For example:
M= 1 2 V=1
2 3 2
4 3 1
2 4 2
should become
M =1 2
2 3
2 3
4 3
2 4
2 4
Does anyone have advice on how to accomplish this? Many thanks.

Best Answer

One way:
f = @(k) repmat(M(k,:), round(V(k)), 1);
MM = cell2mat(arrayfun(f, (1:length(V))', 'UniformOutput', false));