create matrices based on a label sequence
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Andrei Bobrov
on 12 Mar 2014
Edited: Andrei Bobrov
on 12 Mar 2014
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 Comments
More Answers (0)
See Also
Categories
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!