MATLAB Answers

yoga z

using plot in k-means

Asked by yoga z
on 5 Jun 2013
Latest activity Commented on by Kawther
on 30 Nov 2014

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

      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);     
      hold on
      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


on 5 Jun 2013

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
on 8 Jun 2013

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


3 Answers

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
hold on;
legend('Cluster 1','Cluster 2','Cluster 3','Centroids');
hold off;

  1 Comment

Tom Lane
on 9 Jun 2013

You may also want to consider the gscatter function for plotting in two dimensions. It just simplifies some code, but it does not deal with the issue that Jing points out.

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?


Answer by Kawther
on 30 Nov 2014
Edited by Kawther
on 30 Nov 2014

Dear All.

Considering that the clusters represent decision region for each sent symbol, how can i determine the decision region, and how can i find the bet error rate for the clusters resulting from the Kmean?

Thank you Kawther Hamad,

  1 Comment

on 30 Nov 2014

Can i consider the originally sent data as a training data and resend data again and consider it and a test data and use them to find the bet error rate?

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!