Splitting data using kmeans idx

2 vues (au cours des 30 derniers jours)
cee878
cee878 le 17 Juin 2018
Réponse apportée : KSSV le 18 Juin 2018
I'm using the built-in kmeans function and want to split the given data X based on the cluster it belongs to. For example, if I choose k = 5, then I want to put append all of the X data corresponding to cluster 1 into one matrix. What's the best way to do this?

Réponses (1)

KSSV
KSSV le 18 Juin 2018
Check the below demo code:
% A random data
N = 10000 ;
x = rand(N,1) ;
y = rand(N,1) ;
% Number of classes
NC = 10 ;
% apply kmeans
idx = kmeans([x y],NC) ;
% cluster the data
data= cell(NC,1) ;
figure
hold on
for i = 1:NC
data{i} = [x(idx==i),y(idx==i)] ;
plot(data{i}(:,1),data{i}(:,2),'.','color',rand(1,3))
end

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by