I am having a problem in Matlab. I would like to use kmeans clustering and then get the value and index of the centroid. For example, if there is an $5*5$ array, we do kmeans clustering where k=2 and we get 2 centroids. How can I get these values for these centroids? I am currently working depth in matlab and would like to implement a research paper for this.

  • $\begingroup$ i already used ke k-means fuction , build-in in maltab, doesnt give me those indexes $\endgroup$ – arslan ahmed Jan 31 '20 at 18:50
  • 1
    $\begingroup$ Try and gather a better understanding of the algorithm. It is quite an easy to understand procedure. en.wikipedia.org/wiki/K-means_clustering Also, please check your punctuation, it is hard to understand your question. $\endgroup$ – VincFort Jan 31 '20 at 19:17

From the document: https://www.mathworks.com/help/stats/kmeans.html

idx = kmeans(X,k)
idx = kmeans(X,k,Name,Value)
[idx,C] = kmeans(___)
[idx,C,sumd] = kmeans(___)
[idx,C,sumd,D] = kmeans(___)

idx = kmeans(X,k) performs k-means clustering to partition the observations of the n-by-p data matrix X into k clusters, and returns an n-by-1 vector (idx) containing cluster indices of each observation. Rows of X correspond to points and columns correspond to variables.

By default, kmeans uses the squared Euclidean distance metric and the k-means++ algorithm for cluster center initialization.

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

The code is:

[idx,C] = kmeans(X,3);
  • $\begingroup$ i used this matlab fuction , in this function 2 things are returned , idx and C , idx tell us about which value belong to which clusterr i.e 1st one to 1st cluster etc and C is k-by-p matrix having centroid , where k is the number of cluster u want and p is ? sorry i am new to this , what i am trying to do is to give him a big array like 1024*768 and the call this kmeans function , but when the result come it will give 2*768 array of C centorid, as mention kmeans(X,k) performs k-means clustering to partition the observations of the n-by-p data matrix X into k clusters, i only want 2 values $\endgroup$ – arslan ahmed Feb 1 '20 at 15:04
  • $\begingroup$ p is the feature dimension, the column number, so it is 768 in your case. n is observation number, the row number. So you have n=1024 and p=768. You didn't get two values, you get two vectors (don't mistake this), and each has 768 dimensions. The centroid is not value it is the "coordinate". $\endgroup$ – Bill Chen Feb 1 '20 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.