-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

3
  • 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. Commented May 31, 2016 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? Commented May 31, 2016 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. Commented May 31, 2016 at 19:34

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
What do the extra brackets do?
@ErinGoBragh It just specifies the repetition scheme using a row vector. See the documentation of repmat if you haven't yet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.