필터 지우기
필터 지우기

create matrices based on a label sequence

조회 수: 1 (최근 30일)
pavlos
pavlos 2014년 3월 12일
편집: Andrei Bobrov 2014년 3월 12일
Hello,
Please help me with the following:
Consider a 100x10 matrix, called A and a 100x1 vector, called B, that contains labels that refer to the rows of A.
These labels seperate the rows of A, actually they are labels generated by a clustering process.
For example if the number of labels is 3 (and is randomly distributed in B), then all the rows of A are separated in 3 clusters.
With the following commands, we have 3 separate matrices, 1 matrix per cluster:
Cluster1=A(B==1,:);
Cluster2=A(B==2,:);
Cluster3=A(B==3,:);
How can I form different matrices that refer to the different labels avoiding writing 1 command for 1 cluster (maybe with a "for" loop)?
For example, if we have 12 clusters (labels of B), we should avoid writing
cluster1=...
cluster2=...
. .
.
cluster12=...
and the 12 separate matrices (cluster1,...,cluster12) automatically generated.
Thank you very much.
Best,
Pavlos

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 3월 12일
편집: Andrei Bobrov 2014년 3월 12일
claster_n = accumarray(B,1:numel(B),[],@(x){A(x,:)});
or with arrayfun
claster_n = arrayfun(@(x)A(x == B,:),(1:max(B))','un',0);
and with for..end loop
n = max(B);
claster_n = cell(n,1);
for jj = 1:n
claster_n{jj} = A(jj == B,:);
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by