2010-12-09 244 views

回答

5

的“蛮力办法”,如@Dima提到会去如下

%# loop through all clusters 
for iCluster = 1:max(IDX) 
    %# find the points that are part of the current cluster 
    currentPointIdx = find(IDX==iCluster); 
    %# find the index (among points in the cluster) 
    %# of the point that has the smallest Euclidean distance from the centroid 
    %# bsxfun subtracts coordinates, then you sum the squares of 
    %# the distance vectors, then you take the minimum 
    [~,minIdx] = min(sum(bsxfun(@minus,X(currentPointIdx,:),C(iCluster,:)).^2,2)); 
    %# store the index into X (among all the points) 
    closestIdx(iCluster) = currentPointIdx(minIdx); 
end 

要获得最接近聚类中心k点的坐标,使用

X(closestIdx(k),:) 
+2

+1正是我要发布的内容。一个小编辑,我认为应该是:`C(iCluster,:)`而不是`C(iCluster)` – Amro 2010-12-09 16:06:45

1

蛮力的方法是运行k-means,然后比较集群中的每个数据点与质心,并找到最接近它的那个。这在matlab中很容易实现。

另一方面,您可能想要尝试使用k-medoids聚类算法,该算法为您提供数据点作为每个群集的“中心”。这是一个matlab implementation

0

事实上,如果我理解你的话,kmeans已经给你答案了:

[IDX,C, ~, D] = kmeans(X,k); % D is the distance of each datapoint to each of the clusters 
[minD, indMinD] = min(D); % indMinD(i) is the index (in X) of closest point to the i-th centroid 
相关问题