-3

I have a large amount of data. I have implemented k-means clustering on my data. It clustered the data and plot the graph having data points around centroid in the cluster.

The graph is the following:

enter image description here

But I want to get the exact x and y coordinates of the centroid printed so that I can calculate the distance of any new data point from the centroids so as to find out the cluster to which new data will belong.

[ids ctrs]=kmeans(A,19) D = dist([testpoint;ctrs]) %testpoint is 1x10 and D will be 20x20 [distance testpointID] = min(D(1,2:end)) I am not able to understand what [distance testpointID] = min(D(1,2:end)) actually does??Please help me!!!

AlessioX
  • 3,167
  • 6
  • 24
  • 40
user5939997
  • 1
  • 1
  • 2

2 Answers2

2

This is from the Matlab help for the kmeans function

[idx,C] = kmeans(___) % returns the k cluster centroid locations
                      % in the k-by-p matrix C.

This means you can call kmeans with two output arguments. The first one will contain the indeces to your points, the second one the centroid locations you are looking for.

wsj
  • 677
  • 7
  • 11
1

If you've used the Matlab function, then the centroids are the second output argument. However, if you've implemented K-Means by yourself you must manually evaluate such centroids by considering their definition. For a given cluster, the centroid is nothing more then the average value between the points associated to such cluster.
If you have the assignments {point;cluster} you can easily evaluate the centroid: let's say you have a given cluster with n points assigned to it and these points are a1,a2,...,an. You can evaluate the centroid for such cluster by using:

centroid=(a1+a2+...+an)/n 
AlessioX
  • 3,167
  • 6
  • 24
  • 40