## K-means: How to sort the cluster number?

Asked by Alvi Syahrin

### Alvi Syahrin (view profile)

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];
else
% initial value of centroid
if isRand,
p = randperm(size(cobat,1));      % random initialization
for i=1:k
c(i,:)=cobat(p(i),:) ;
end
else
for i=1:k
c(i,:)=cobat(i,:);        % sequential initialization
end
end
```
```	temp=zeros(maxRow,1);   % initialize as zero vector
u=0;
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
else
temp=g;             % copy the matrix to the temporary variable
end
for i=1:k
f=find(g==i);
if f                % calculate the new centroid
c(i,:)=mean(cobat(find(g==i),:),1)
end
end
c
sort(c)
end
y=[cobat,g]```

"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?

## Products

No products are associated with this question.

### the cyclist (view profile)

Answer by the cyclist

### the cyclist (view profile)

on 5 May 2013

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

Alvi Syahrin

### Alvi Syahrin (view profile)

on 6 May 2013

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

### Tom Lane (view profile)

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.

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