4

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 ?

1 Answer 1

5

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.

1
  • 2
    @HamedHamed: You can simply call repmat({mat},[1,1,n]) if the output should be a cell array.
    – Jonas
    Dec 25, 2012 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.