MATLAB中Kmeans聚类中最接近中心点的数据点索引的获取
我正在用MATLAB中的K-means进行聚类。如您所知,用法如下:MATLAB中Kmeans聚类中最接近中心点的数据点索引的获取,matlab,cluster-analysis,k-means,Matlab,Cluster Analysis,K Means,我正在用MATLAB中的K-means进行聚类。如您所知,用法如下: [IDX,C] = kmeans(X,k) 其中,IDX给出X中每个数据点的簇号,C给出每个簇的质心。我需要得到距离质心最近的数据点的索引(实际数据集X中的行号)。有人知道我怎么做吗? 多亏了蛮力方法是运行k-means,然后将集群中的每个数据点与质心进行比较,并找到最接近质心的数据点。这在matlab中很容易做到 另一方面,您可能希望尝试聚类算法,它为您提供一个数据点作为每个集群的“中心”。下面是一个。的“暴力方法”,如
[IDX,C] = kmeans(X,k)
其中,IDX给出X中每个数据点的簇号,C给出每个簇的质心。我需要得到距离质心最近的数据点的索引(实际数据集X中的行号)。有人知道我怎么做吗?
多亏了蛮力方法是运行k-means,然后将集群中的每个数据点与质心进行比较,并找到最接近质心的数据点。这在matlab中很容易做到 另一方面,您可能希望尝试聚类算法,它为您提供一个数据点作为每个集群的“中心”。下面是一个。的“暴力方法”,如
%# 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
要获取最靠近群集中心的点的坐标,请使用
X(closestIdx(k),:)
事实上,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