MATLAB: Repeat element of a vector n times without loop.

repeat vector

Say I have a column vector x=[a;b;c]. I want to repeat each element n times to make a long length(x)*n vector. For example, for n=3, the answer would be:
ans=
a
a
a
b
b
b
c
c
c
Can anyone think of an elegant way to do this without looping?
Thanks,
Justin

Best Answer

n=3 ; x=(1:3)' % example
r=repmat(x,1,n)';
r=r(:)'