# Store every N columns in a Matrix to a new Matrix

2 views (last 30 days)
Ben Whitby on 11 May 2022
Edited: Ben Whitby on 8 Jun 2022
Hi All,
I have a large Matrix, 15 x 688128. I need to iterate through the matrix saving each set of 4096 columns to a new matrix such that I end up with 168 new matrices each 15 x 4096 in size
S1 contains columns 1 to 4096
S2 contains columns 4097 - 8192
etc such that the final matrix contains columns 684032 to 688128.
I have the loop below but this obviously overwrites the matrix on each iteration through the loop so I only end up with one matrix rather than 168 of them...How can I save the matrix on each iteration?
for nn = 0:688128
CorBeam13 = CorBeam1(:,nn*4096+1:(nn+1)*4096); %Take Vales for each ensemble period. Each ensemble period is 17.0667 secs long and contains 4096 samples
end

James Tursa on 11 May 2022
Edited: James Tursa on 12 May 2022
Don't create a bunch of new matrices with hard to use names downstream in your code. Instead, simply reshape your matrix and then use indexing. E.g.,
CorBeam13 = reshape(CorBeam13,15,4096,[]);
Then downstream in your code, instead of S1, S2, etc., simply use CorBeam13(:,:,1), CorBeam13(:,:,2), etc.
If for some reason you really want them split up, you could convert them to a cell array of matrices with the mat2cell( ) function, which would still allow you to use easy indexing downstream in your code.
Ben Whitby on 17 May 2022
Thanks, with some adjsutments to my original code this does work. The link to the documentation was also useful

### Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by