# Dividing a Matrix into submatrices according to a specific column value

17 views (last 30 days)
Michail Isaakidis on 10 Jan 2019
Commented: Michail Isaakidis on 11 Jan 2019
Hi all!
So I have a very big matrix (10000x6 elements) with all my data and one of the columns contains the group that each set of data belongs (from 1 to 500). How would it be possible to seperate them with into individual matrices with the use of the group number?

rakbar on 10 Jan 2019
Use MATLAB findgroup and splitapply functions. Here is an example:
https://www.mathworks.com/help/matlab/ref/splitapply.html#bux2_9d
% create a random matrix 10 x 4
A = rand(10,4);
% the 5th column is the "Gourp" column
A(:,5) = [ 1 1 2 2 3 3 4 4 5 5 ];
% Find the "Grouping" vector
G = findgroups(A(:,5));
% function to return gourped sum matrix
func = @(x){(x)};
% return each submatrix (cell array)
Y = splitapply(func,A,G);
Michail Isaakidis on 11 Jan 2019
Thank you very much! That solved my problem!

Akira Agata on 10 Jan 2019
The following is an another way that is easy to understand intuitively:
% create a random matrix 10 x 4
A = rand(10,4);
% the 5th column is the "Gourp" column
A(:,5) = [ 1 1 2 2 3 3 4 4 5 5 ];
% Find unique values
ID = unique(A(:,5));
% Separate the matrix
Y = cell(size(ID));
for kk = 1:numel(ID)
idx = A(:,5) == ID(kk);
Y{kk} = A(idx,:);
end