Hamed Hamed
Hamed Hamed

Reputation: 43

How to Concatenate arrays 'n' time in matlab?

The commands:

>> mat = magic( 4 );
>> out = cat( 3, mat, mat );

concatenate matrix 'mat', along third dimension, 2 time and produce a 4-by-4-by-2 array

how to do this work 'n' time an produce a 4-by-4-by-n array without using loop?

I know this is possible by using a cell array like this:

>> out = cat( 3, cellArray{:} );

but how to create this cell array? :

>> cellArray = {mat, mat, ... , mat};  % n time

how to Concatenate arrays n time in matlab ?

Upvotes: 4

Views: 2845

Answers (1)

HebeleHododo
HebeleHododo

Reputation: 3640

You can use repmat.

If you want a 3-dimensional matrix:

mat = magic(4);
n = 3; % Number of times you want to replicate
out = repmat(mat,[1 1 n]);

out will be a 4x4xn double array.

If you want a cell array you can do this as an additional step:

outCell = mat2cell(out,4,4,ones(1,n));

outCell will be a 1x1xn cell array.

Upvotes: 5

Related Questions