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.
Asked
Active
Viewed 1,444 times
3 Answers
1
Another way to do it:
A2=A(:,ones(1,n));
B=A2(:)

Mendi Barel
- 3,350
- 1
- 23
- 24
-
This does replicate the vector `A` but not in the desired direction of the OP. – mpaskov Mar 03 '17 at 21:50
-
1I didn't noticed the column result. so i added some hack in the answer – Mendi Barel Mar 03 '17 at 22:15
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