create matrices based on a label sequence
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!