%This demonstrates the kmeans algorithm for clustering points.
%REQUIRES MATLAB's MACHINE LEARNING TOOLBOX (not included in Mathscript).
clear;rng default:%Generate random points in 2 clusters:
X=[randn(100,2)*0.75+0.5*ones(100,2);randn(100,2)*0.5-ones(100,2)];
figure;plot(X(:,1),X(:,2),'.'),title('Random points in 2 clusters')
opts=statset('Display','final');
[idx,C]=kmeans(X,2,'MaxIter',4,'Replicates',1,'Options',opts);
figure,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(C(:,1),C(:,2),'kx','MarkerSize',15,'LineWidth',3)
legend('Cluster 1','Cluster 2','Centroids','Location','NW')
title('Cluster Assignments and Centroids'),hold off