フィルターのクリア

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 Center および File ExchangeParallel Computing Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by