MATLAB: How to save all iteration matrices and put it into big one

loopsave

I have a matrix (Q) 1140 by 330. I have to divide the matrix into 60 matrices 19 by 330. and then calculate the correlation coefficient of each one. my code is in the below:
splitQ = mat2cell(Q, ones(1, 60) * 19, 330);
for i=1:60
correlationCoefficient= corrcoef(splitQ{i,1});
end
Now I need to save all the resulted matrices from the loop in a new matrix. but the loop save the final valu!
does anyone know how to do it?

Best Answer

You don't have to create splitQ, you can work on Q directly:
ind = 1:19:size(Q,1); % create the starting indices of the rows
R = nan(size(R,2), size(R, 2), numel(ind)); % preallocate resulting matrix for speed
for i = 1:numel(ind)
R(:,:,i) = corrcoef(Q(ind(i):ind(i)+18,:)); % compute the i'th correlation matrix
end
You can then access the i'th correlation matrix as
R(:,:,i)