フィルターのクリア

Dividing a Matrix into submatrices according to a specific column value

14 ビュー (過去 30 日間)
Michail Isaakidis
Michail Isaakidis 2019 年 1 月 10 日
コメント済み: Michail Isaakidis 2019 年 1 月 11 日
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?
Thank you in advance.

採用された回答

rakbar
rakbar 2019 年 1 月 10 日
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);

その他の回答 (1 件)

Akira Agata
Akira Agata 2019 年 1 月 10 日
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

カテゴリ

Help Center および File ExchangeTime Series についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by