Divide a Large Matrix into Smaller Matrices in a Loop

Hi all,
I have a Matrix of dimensions 20x5.
I need to run calculations, using For loops, on a program that first needs the first 4 lines of the large matrix, then the next 4 lines etc...
In other terms: 1 iteration ---> Large matrix lines [1 to 4 x 5] .... used for the calculations. Then 2 iteration ---> Large matrix lines [5 to 8 x 5] .... used for 2nd iteration calculations. Then 3 iteration ---> Large matrix lines [9 to 12 x 5].... used for 3rd iteration calculations etc...
Does anyone has an idea on how to make it happen?

 채택된 답변

Stephen23
Stephen23 2017년 11월 21일
편집: Stephen23 2017년 11월 21일
You can define the indexing quite easily:
mat = rand(20,5);
row = 4; % rows per group
grp = size(mat,1)/row;
for k = 1:grp
idr = (1:row)+(k-1)*row;
mat(idr,:)
end
Or split the data into a cell array using mat2cell:
C = mat2cell(mat,row*ones(1,grp),size(mat,2));
for k = 1:grp
C{k}
end

댓글 수: 4

Nicolas Jalbert
Nicolas Jalbert 2017년 11월 21일
편집: Nicolas Jalbert 2017년 11월 21일
Stephen,
Thanks for your answer. I need to use the first one, since I need the matrices for the calculations.
One question remains: would it be possible to "store" this 5 news matrices created? ---> In the program I would then call one small matrix at a time via let's say: mat(1), then I call mat(2) ... This would render the program more simple since I could perform this matrix division before entering the calculations.
I would then only include in the calculations a for loop calling each of the small matrix at a time.
Would it be possible?
Stephen23
Stephen23 2017년 11월 21일
편집: Stephen23 2017년 11월 21일
"Would it be possible?"
Yes. That is exactly what the second method shows you how to do: C is a cell array containing all of the submatrices. Access them using cell array indexing:
C{1}
C{2}
etc
Or, if you really want to use indexing rather than mat2cell, then just put them into a cell array explicitly:
C = cell(1,grp);
for k = 1:grp
idr = (1:row)+(k-1)*row;
C{k} = mat(idr,:);
end
I found a way without having to use cells arrays, this way we keep the matrices up for use. Because if they are stored in a cell array, we need a conversion (cell2mat?) to perform calculations with them right?
Here is my program:
mat = rand(20,5)
row = 4 % rows per group
grp = size(mat,1)/row
for k = 1:grp
idr = (1:row)+(k-1)*row;
mat(idr,:);
Matrix(:,:,k) = mat(idr,:);
end
Matrix
Is this a proper way to store these matrices? Or is there more elegant?
Stephen23
Stephen23 2017년 11월 21일
편집: Stephen23 2017년 11월 21일
"Because if they are stored in a cell array, we need a conversion (cell2mat?) to perform calculations with them right?"
Not at all. You just need to use indexing, or something like cellfun.
"Is this a proper way to store these matrices?"
What is your definition of "proper"? The best way to store data is determined by the nature of that data and how is going to be processed, neither of what you have said anything about. Keeping that data in one numeric array will probably make numeric calculation a lot simpler, as you could write vectorized code.
"Or is there more elegant?"
An elegant way to rearrange that matrix into a 3D array is to use reshape and permute, no loop is required:
out = permute(reshape(mat.',[],row,grp),[2,1,3])

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 11월 21일
편집: Andrei Bobrov 2017년 11월 21일
mat1 - matrix with size (20,5)
row1 = 4;
Matrix1 = permute(reshape(mat1.',size(mat1,2),row1,[]),[2,1,3]);

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2017년 11월 21일

편집:

2017년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by