MATLAB: duplicating vector 'n' times

matlab, vector

Solution

Try

repmat([1 2 3],1,3)

I'll leave you to check the documentation for `repmat`.

Problem

I have a vector, e.g. ``` vector = [1 2 3] ``` I would like to duplicate it within itself n times, i.e. if n = 3, it would end up as: ``` vector = [1 2 3 1 2 3 1 2 3] ``` How can I achieve this for any value of n? I know I could do the following: ``` newvector = vector; for i = 1 : n-1 newvector = [newvector vector]; end ``` This seems a little cumbersome though. Any more efficient methods?

Original source

Related problems