-2

I'm trying to take:

a = [1 2 3]  

and repeat it 5 times to get:

b = [1 2 3 1 2 3 1 2 3 1 2 3 1 2 3]  

but when I try:

b = repmat(a, 5, 1)  

instead I get:

 b =

 1     2     3
 1     2     3
 1     2     3
 1     2     3
 1     2     3

I could probably do it with a for loop but I'd like to do it correctly if possible. Any suggestions? Thanks in advance

ErinGoBragh
  • 336
  • 4
  • 20
  • 1
    Please make an effort to do a search for your question next time. By searching for "`MATLAB repeat vector stackoverflow`" on Google, the duplicate I've marked was the first link I've found. – rayryeng May 31 '16 at 16:59
  • I apologize. I did search, I used a wrong term (array vs vector) so I didn't find it. Should I take the question down? – ErinGoBragh May 31 '16 at 19:33
  • Nah don't worry. I think this is productive as people may search for the same keywords as seen in your title. I say leave it. BTW, I didn't downvote you. – rayryeng May 31 '16 at 19:34

1 Answers1

2

Use the following code:

b = repmat(a,1,5)

The numbers '1' and '5' refer to the amount of rows and columns that you want to repeat the matrix a. The order is important.

tvo
  • 780
  • 7
  • 24
  • `b=repmat(a,[1,5])` would be another way to write it. Maybe it would be good to mention, that the second argument refers to the first dimension which is vertical. The third argument refers to the second dimension, which is horizontal. – Matt May 31 '16 at 16:51
  • What do the extra brackets do? – ErinGoBragh May 31 '16 at 16:55
  • @ErinGoBragh It just specifies the repetition scheme using a row vector. See the documentation of [`repmat`](http://www.mathworks.com/help/matlab/ref/repmat.html) if you haven't yet. – Matt May 31 '16 at 17:05