Splitting data using kmeans idx

2 views (last 30 days)
cee878
cee878 on 17 Jun 2018
Answered: KSSV on 18 Jun 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?

Answers (1)

KSSV
KSSV on 18 Jun 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!