% I took this code from the Matlab help available at:
% Statistics Toolbox -> Functions -> Cluster Analysis -> K-Means Clustering -> kmeans 

clear; close

X = [randn(100,2)+ones(100,2);...
     randn(100,2)-ones(100,2)];

opts = statset('Display','final');


%% For 2 clusters, use the instructions below:

[idx,ctrs] = kmeans(X,2);

plot(X(idx==1,1),X(idx==1,2),'r.','MarkerSize',12)
hold on
plot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',12)
plot(ctrs(:,1),ctrs(:,2),'kx',...
     'MarkerSize',12,'LineWidth',2)
plot(ctrs(:,1),ctrs(:,2),'ko',...
     'MarkerSize',12,'LineWidth',2)
legend('Cluster 1','Cluster 2','Centroids',...
       'Location','NW');


%% For 3 clusters, use the instructions below:
%% 
%% [idx,ctrs] = kmeans(X,3);
%% 
%% plot(X(idx==1,1),X(idx==1,2),'r.','MarkerSize',12)
%% hold on
%% plot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',12)
%% plot(X(idx==3,1),X(idx==3,2),'g.','MarkerSize',12)
%% plot(ctrs(:,1),ctrs(:,2),'kx',...
%%      'MarkerSize',12,'LineWidth',2)
%% plot(ctrs(:,1),ctrs(:,2),'ko',...
%%      'MarkerSize',12,'LineWidth',2)
%% legend('Cluster 1','Cluster 2','Cluster 3','Centroids',...
%%        'Location','NW');


%% SEE THE HELP'S WEBPAGE FOR OTHER K-MEANS CLUSTERING OPTIONS


