axel
axel

Reputation: 15

How to create a Matlab array given another array

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

Upvotes: 0

Views: 391

Answers (1)

avermaet
avermaet

Reputation: 1593

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)

Upvotes: 2

Related Questions