Splitting data using kmeans idx

2 次查看(过去 30 天)
cee878
cee878 2018-6-17
回答: KSSV 2018-6-18
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?

回答(1 个)

KSSV
KSSV 2018-6-18
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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by