2

I would like to create, from the column matrix A=[1;2;3], another column matrix that repeats A n times. For example, being n=3, the new matrix would be B=[1;2;3;1;2;3;1;2;3]. Is there a way to to that (preferably without using loops)? Thank you.

bru1987
  • 56
  • 2
  • 14

3 Answers3

4

You can use repmat it is a fantastic function:

repmat(A,[n,1])

The first value of the second parameter is repetitions in the first dimension (columns), the second in the second dimension (rows) etc.

mpaskov
  • 1,234
  • 2
  • 11
  • 21
1

Another way to do it:

A2=A(:,ones(1,n));
B=A2(:)
Mendi Barel
  • 3,350
  • 1
  • 23
  • 24
0

Another way is to do using padarray.

a = [1 2 3]
b = padarray(a, [2 0], 'post', 'circular')

post means add to the end of the array, circular pads with circular repetition of elements.

smttsp
  • 4,011
  • 3
  • 33
  • 62