Hos*_*ein 4 matlab cluster-analysis k-means
我在MATLAB中使用K-means进行一些聚类.您可能知道用法如下:
[IDX,C] = kmeans(X,k)
Run Code Online (Sandbox Code Playgroud)
其中IDX给出X中每个数据点的簇号,C给出每个簇的质心.我需要得到距离质心最近的数据点的索引(实际数据集X中的行号).有谁知道我怎么做到这一点?谢谢
@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
Run Code Online (Sandbox Code Playgroud)
要获取最接近群集中心的点的坐标k,请使用
X(closestIdx(k),:)
Run Code Online (Sandbox Code Playgroud)