
%%%%%2 clusters
DATA=[X',Y'];
opts = statset('Display','final');
%%%调用聚类函数
[Idx2,Ctrs2,SumD2,D2] = kmeans(DATA,2,'Replicates',2,'Options',opts);

%画出聚类为1的点。X(Idx==1,1),为第一类的样本的第一个坐标；X(Idx==1,2)为第二类的样本的第二个坐标
figure(2)
plot(DATA(Idx2==1,1),DATA(Idx2==1,2),'r.','MarkerSize',14);
hold on
plot(DATA(Idx2==2,1),DATA(Idx2==2,2),'b.','MarkerSize',14);
hold on


%绘出聚类中心点,kx表示是圆形
plot(Ctrs2(:,1),Ctrs2(:,2),'kx','MarkerSize',14,'LineWidth',4);
plot(Ctrs2(:,1),Ctrs2(:,2),'kx','MarkerSize',14,'LineWidth',4);
hold on 
%%%标注
legend('Cluster 1','Cluster 2','Centroids','Location','NW');

Ctrs2;
SumD2;




%%%3 clusters
DATA=[X',Y'];
opts = statset('Display','final');
%%%调用聚类函数
[Idx3,Ctrs3,SumD3,D3] = kmeans(DATA,3,'Replicates',3,'Options',opts);

%画出聚类为1的点。X(Idx==1,1),为第一类的样本的第一个坐标；X(Idx==1,2)为第二类的样本的第二个坐标
figure(3)
plot(DATA(Idx3==1,1),DATA(Idx3==1,2),'r.','MarkerSize',14);
hold on
plot(DATA(Idx3==2,1),DATA(Idx3==2,2),'b.','MarkerSize',14);
hold on
plot(DATA(Idx3==3,1),DATA(Idx3==3,2),'g.','MarkerSize',14);
hold on


%绘出聚类中心点,kx表示是圆形
plot(Ctrs3(:,1),Ctrs3(:,2),'kx','MarkerSize',14,'LineWidth',4);
plot(Ctrs3(:,1),Ctrs3(:,2),'kx','MarkerSize',14,'LineWidth',4);
plot(Ctrs3(:,1),Ctrs3(:,2),'kx','MarkerSize',14,'LineWidth',4);
hold on 
%%%标注
legend('Cluster 1','Cluster 2','Cluster 3','Centroids','Location','NW');

Ctrs3;
SumD3;


%%%4 clusters 
DATA=[X',Y'];
opts = statset('Display','final');
%%%调用聚类函数 
[Idx4,Ctrs4,SumD4,D4] = kmeans(DATA,4,'Replicates',4,'Options',opts);

%画出聚类为1的点。X(Idx==1,1),为第一类的样本的第一个坐标；X(Idx==1,2)为第二类的样本的第二个坐标
figure(4)
plot(DATA(Idx4==1,1),DATA(Idx4==1,2),'r.','MarkerSize',14);
hold on
plot(DATA(Idx4==2,1),DATA(Idx4==2,2),'b.','MarkerSize',14);
hold on
plot(DATA(Idx4==3,1),DATA(Idx4==3,2),'g.','MarkerSize',14);
hold on
plot(DATA(Idx4==4,1),DATA(Idx4==4,2),'y.','MarkerSize',14);
hold on

%绘出聚类中心点,kx表示是圆形
plot(Ctrs4(:,1),Ctrs4(:,2),'kx','MarkerSize',14,'LineWidth',4);
plot(Ctrs4(:,1),Ctrs4(:,2),'kx','MarkerSize',14,'LineWidth',4);
plot(Ctrs4(:,1),Ctrs4(:,2),'kx','MarkerSize',14,'LineWidth',4);
plot(Ctrs4(:,1),Ctrs4(:,2),'kx','MarkerSize',14,'LineWidth',4);
hold on 
%%%标注
legend('Cluster 1','Cluster 2','Cluster 3','Cluster 4','Centroids','Location','NW');

Ctrs4;
SumD4;



