2
votes

I have a cell array C containing 380 matrices and want to put them in 3-dimensional matrix M such that the 1st 20 matrices of C will be in M(:,:,1), the 2nd 20 matrices (from 21 to 40) in M(:,:,2) and so on.

Edit: I want to concatenate them columnwise.

Can I do this in a one loop in Matlab.

1
Do you want to concatenate 20 matrices rowwise or columnwise? - petrichor
you have the right idea, just wrap it in a for loop for i = 1:20:380 and use the indexed assignment operator as you showed in your question to organize the elements as you wish. If the dimensions are not so neat, you may have to nest a loop to deal with the 20 individual matrices for j=1:20 and slot them into the correct positions - im so confused

1 Answers

6
votes

You don't even need a loop for that:

%# C: cell array with 380 elements

%# reshape C to 20x1xn
C = reshape(C,20,1,[]);

%# create matrix M
M = cell2mat(C);