0

Let us assume a simple array A

A = [1 2 3 4 5 6 7 8];

I would like to create an array B that will contain the A as many as 2 times:

B = [A A]

Then B, will be of dimensions (1,2*length(A))

How can I do the same, but for N times (e.g. using a for loop or something like this)?

for i = 1:N
    B = ???
end

so that

B = [A A A.....A]

I tried repmat to make first the B as a matrix, and then reshape. However reshape does not work as I was expecting and instead of giving:

1     2     3     4     5     6     7     8     1     2     3     4     
5     6     7     8

it gave:

1     1     2     2     3     3     4     4     5     5     6     6     
7     7     8     8
0

1 Answer 1

2

You need to keep stacking them, like: B = [B A] inside the loop. Or even better, use the function repmat() which stacks them in a single function call. In your case of row-major stacking:

n = 100; % for 100 reps
B = repmat(A,1,n)
Sign up to request clarification or add additional context in comments.

1 Comment

Yep! I didn't think of using repmat in this specific way! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.