Store every N columns in a Matrix to a new Matrix

조회 수: 9 (최근 30일)
Ben Whitby
Ben Whitby 2022년 5월 11일
편집: Ben Whitby 2022년 6월 8일
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
James Tursa 2022년 5월 11일
편집: James Tursa 2022년 5월 12일
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.
See this link:
  댓글 수: 1
Ben Whitby
Ben Whitby 2022년 5월 17일
Thanks, with some adjsutments to my original code this does work. The link to the documentation was also useful

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by