Asked by yoga z
on 5 Jun 2013

i try to applly k-means using data like this:

x=[ 3.4600 3.8700 3.6100 1.0000 1.0000 2.0000 ] [ 3.0900 3.3400 3.2200 2.0000 0 1.0000] [ 3.3300 3.0300 3.1900 1.0000 1.0000 1.0000] [ 3.5600 3.3400 3.4900 2.0000 2.0000 1.0000] [ 3.2600 3.1500 3.2000 1.0000 0 0] [ 3.1900 3.3600 3.3700 1.0000 0 1.0000] [ 3.1600 3.2100 3.1400 1.0000 1.0000 1.0000] [ 3.7600 3.6200 3.5800 0 1.0000 1.0000] [ 3.0900 3.1500 2.9300 1.0000 1.0000 1.0000] [ 3.6300 3.6900 3.5600 0 0 1.0000] [ 3.0700 3.0800 2.8600 0 1.0000 1.0000]

and i write this code in matlab

clear; clc; x=xlsread('test.xlsx',1,'C3:H159'); k=3; p=200; opts = statset('Display','final'); %[idx,ctrs,sumd] = kmeans(x,k,'Distance','city','Replicates',p,'Options',opts,'start','uniform','emptyaction','drop'); [idx,ctrs,sumd] = kmeans(x,k,'Distance','city','Replicates',p,'Options',opts); 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');

but results are illustrated using plot function is irregular, in the sense that the object is not clustered with either. any one can help me to improve this code, thanx b4

Answer by Jing
on 9 Jun 2013

Like I said above, first of all, you need to decide which dimensions you want to show your clusters. You can't show the clusters in 6 dimension. Say, you want to show in the first two dimension, then your code is right for that. The reason you find 'the plot is irregular', may be the first two dimension is far from enough to determine the centroid. To show more dimension in a figure, you may also use plot3 to include 3 dimensions:

dm=[1,2,3];% draw first 3 dimension plot3(x(idx==1,dm(1)),x(idx==1,dm(2)),x(idx==1,dm(3)),'r.','MarkerSize',12); hold on; plot3(x(idx==2,dm(1)),x(idx==2,dm(2)),x(idx==2,dm(3)),'b.','MarkerSize',12); plot3(x(idx==3,dm(1)),x(idx==3,dm(2)),x(idx==3,dm(3)),'g.','MarkerSize',12); plot3(ctrs(:,dm(1)),ctrs(:,dm(2)),ctrs(:,dm(3)),'ko','MarkerSize',12,'LineWidth',2); legend('Cluster 1','Cluster 2','Cluster 3','Centroids'); hold off;

Answer by Kawther
on 30 Nov 2014

What if i wanted to plot 4 clusters. should i plot in 4 dimensions? if yes, how can i plot?

Opportunities for recent engineering grads.

## 2 Comments

## Jing (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/78056#comment_153001

What are you trying to do with the PLOT? Show the distribution of your data? If so, you need to first determine which dimensions you want to show the distribution in, because you have 6 dimensions in your data, it's impossible to show them all in one plot without reduce the dimensions.

## yoga z (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/78056#comment_153840

thank you for your answer Jing, i just want to show the 3 cluster with their centroid, what i should do?