MATLAB Answers


K-means: How to sort the cluster number?

Asked by Alvi Syahrin on 5 May 2013

I'm working with k-means in matlab. Here is my code:

load cobat.txt;  % read the file
k=input('Enter a number: ');        % determine the number of cluster
isRand=0;   % 0 -> sequeantial initialization
            % 1 -> random initialization
[maxRow, maxCol]=size(cobat);
if maxRow<=k, 
    y=[m, 1:maxRow];
	% initial value of centroid
    if isRand,
        p = randperm(size(cobat,1));      % random initialization
        for i=1:k
            c(i,:)=cobat(p(i),:) ; 
        for i=1:k
           c(i,:)=cobat(i,:);        % sequential initialization
	temp=zeros(maxRow,1);   % initialize as zero vector
	while 1,
        d=DistMatrix3(cobat,c);   % calculate the distance 
        [z,g]=min(d,[],2);      % set the matrix g group
          if g==temp,             % if the iteration doesn't change anymore
              break;              % stop the iteration
              temp=g;             % copy the matrix to the temporary variable
          for i=1:k
              if f                % calculate the new centroid 

"cobat" is the file of mine. Here it looks:

65	80	55
45	75	78
36	67	66
65	78	88
79	80	72
77	85	65
76	77	79
65	67	88
85	76	88
56	76	65

"c" is the variable of centroid (the central of the cluster) per each cluster. "g" is the variable to show the cluster number. The problem is, I want to sort/fit the cluster number (small to big) based on the centroid (c). So, I try to sort(c), but it doesn't affect to the cluster number (g).

When I try to sort(g), it's sorted just not like what I want. I want the cluster number is sorted based on the centroid. Example; when I run the code with k=3, here is the final centroid

   73.0000   79.0000   70.6667 %C 1
   58.3333   73.3333   84.6667 %C 2
   36.0000   67.0000   66.0000 %C 3

When I sort it, the number cluster is also "sorted",

36.0000   67.0000   66.0000 %C 3
58.3333   73.3333   70.6667 %C 2
73.0000   79.0000   84.6667 %C 1

I want it the number cluster is fit, like this.

    36.0000   67.0000   66.0000 %C 1
    58.3333   73.3333   70.6667 %C 2
    73.0000   79.0000   84.6667 %C 3

This seems easy, but tricky. I couldn't figure out. Anyone have any idea to solve it?



No products are associated with this question.

1 Answer

Answer by the cyclist
on 5 May 2013

It sounds like you just need the sortrows() function.


Thank you for the answer. Where do I put that script? I have tried sortrows(c) or sortrows (g) or both, but it's just sorted not like what I want. I want the cluster number (g) is fit to the position of the cluster central (c). And when y=[cobat,g] is run, it doesn't have to be sorted. I just want the cluster number is sorted based on the position of the cluster central (c). You have any idea? ^_^

Tom Lane
on 7 May 2013

Although I may not be following the question, type "help sortrows" and look at the second output. If you need to change the order of another array to match the sorted order, this second output may help.

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!