Asked by huda nawaf
on 1 Aug 2012

hi, i used this function kmeans, and take simple example

x=[100 2 4 10 200; 50 100 20 1 5];

[p o]=kmeans(x,2)

what i got:

p =

1

2

o =

100 2 4 10 200

50 100 20 1 5

what i need is know each point for which cluster belong, what the code do is just get the two clusters i gave it and the points that i gave it.

how I know the clusters that each point belong to?

thanks

*No products are associated with this question.*

Answer by Peter Perkins
on 1 Aug 2012

Accepted answer

Huda, you gave kmeans two points and asked it to cluster them into two clusters. It has assigned the first point to cluster 1, whose centroid is at the first point, and similarly for the second point. Presumably that is not very informative.

I don't know what your data mean, or whether kmeans makes sense, but your description sounds like something more suited to distance-based methods such as hierarchical clustering or multidimensional scaling, both of which are available in the Statistics Toolbox. You would, of course, have to convert your similarities to dissimilarities.

huda nawaf
on 2 Aug 2012

thanks,

i used statistic toolbox

X = [0 4 6;2 0 4;3 5 0);

each row represent rates of similarity of one user with the other, what i need is clustering of users

x=magic(3); c = clusterdata(X,'linkage','ward','savememory','on',... 'maxclust',4); scatter3(X(:,1),X(:,2),X(:,3),10,c)

still ,there is something is not clear, how I know the cluster of each user?

Answer by Walter Roberson
on 2 Aug 2012

The first output from kmeans() **is** the cluster number for each sample.

http://www.mathworks.com/help/toolbox/stats/kmeans.html

IDX = kmeans(X,k) partitions the points in the n-by-p data matrix X into k clusters. This iterative partitioning minimizes the sum, over all clusters, of the within-cluster sums of point-to-cluster-centroid distances. Rows of X correspond to points, columns correspond to variables. **kmeans returns an n-by-1 vector IDX containing the cluster indices of each point**. By default, kmeans uses squared Euclidean distances. When X is a vector, kmeans treats it as an n-by-1 data matrix, regardless of its orientation.

Show 4 older comments

Walter Roberson
on 5 Aug 2012

I do *not* mean that the output will be the number of clusters for each row. I mean that the output is will be the cluster number for each row. "number of clusters" would be "how many clusters occur in that row". "cluster number" is the index of which cluster the row was assigned to.

The cluster numbers have no separate meaning. It does not matter whether the order is "cluster 2 then cluster 3" or "cluster 3 and then cluster 2": the important part is consistency and that the cluster centroid is the right one (you are not outputing the cluster centroids.)

huda nawaf
on 6 Aug 2012

sorry , may be im not clear. I;m also do not mean the no. of clusters. what I mean , I thought that user1 belong to second cluster, user2 belong to third cluster, and user3 belong to second cluster depending on least distance. where each row represet the rate of similarity with other users.

in this case , is clusterdata suitable way for clustering? thanks

Peter Perkins
on 6 Aug 2012

Walter, huda is using hirarchical clustering now, not k-means, so I think he was responding to my earlier post.

huda, again, you have given clusterdata three points,andf asked for three clusters, and it has returned what you asked for. The cluster number is completely arbitrary. I suspect that if you used linkage, then dendrogram, you'd see what you were expecting

d = [0 10 20; 10 0 5; 20 5 0] % d is a distance matrix z = linkage(squareform(d)) % convert d to vector form first dendrogram(z)

The dendrogram plot has labels on its horizontal axis that refer to the original points, not the cluster numbers).

Opportunities for recent engineering grads.

## 2 Comments

## huda nawaf (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45064#comment_92663

to be more clear, if I have array : x=[100 2 4 10 200; 50 100 20 1 5; 3 4 5 6 6;....]; this array show the rate of similarity. where the first row show the similarity rate between user1 and the all other four users, the second row show the rate of similarity between second user and the others and so on. how I know each user for which cluster belong?

thanks

## Yash (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45064#comment_92719

try to plot the result also