Filter löschen
Filter löschen

create matrices based on a label sequence

1 Ansicht (letzte 30 Tage)
pavlos
pavlos am 12 Mär. 2014
Bearbeitet: Andrei Bobrov am 12 Mär. 2014
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

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 12 Mär. 2014
Bearbeitet: Andrei Bobrov am 12 Mär. 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

Weitere Antworten (0)

Kategorien

Mehr zu Parallel Computing Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by