Performance 计算一组点的最近聚类
我有一个问题,我有一个集群中心列表和一个点列表,我必须得到最近的集群 例如: 现在,我可以迭代每个点,并计算哪个簇是最近的簇:Performance 计算一组点的最近聚类,performance,matlab,classification,Performance,Matlab,Classification,我有一个问题,我有一个集群中心列表和一个点列表,我必须得到最近的集群 例如: 现在,我可以迭代每个点,并计算哪个簇是最近的簇: ClusterIndexes = zeros(npoints,1) for i = 1:npoints % sum of squared differences (square root can be avoided) SquareDist = sum(bsxfun(@minus, Clusters, Points(i,:)).^2,
ClusterIndexes = zeros(npoints,1)
for i = 1:npoints
% sum of squared differences (square root can be avoided)
SquareDist = sum(bsxfun(@minus, Clusters, Points(i,:)).^2, 2);
% Index of the closest cluster
[minVal minIdx] = min(SquareDist);
ClusterIndexes(i) = minIdx;
end
% plot results
figure(2), hold on
plot(Clusters(:,1), Clusters(:,2), 'ro')
plot(Points(:,1), Points(:,2), 'bx')
plot([Points(:,1) Clusters(ClusterIndexes,1)]', [Points(:,2) Clusters(ClusterIndexes,2)]', 'm-');
由于我使用的是bsxfun
,因此该代码有效。
然而,有没有更好的方法呢?也许要摆脱这个循环
我在想一个例子,有大量的点,它们可能在高维空间中
谢谢在簇上循环可能比在点上循环更有效,因为这些点较少。嗯,我不知道这会如何工作。对于当前簇,我将获得到所有点的距离,并计算到簇的最近点。但是我需要在循环上存储这个距离矩阵。我认为这将需要更多的内存和更多的操作。此外,也不能保证簇的数量低于点的数量。在问题中,我只是使用了一个玩具的例子。它可能更有效地循环集群而不是点,因为这些点较少。嗯,我不知道这将如何工作。对于当前簇,我将获得到所有点的距离,并计算到簇的最近点。但是我需要在循环上存储这个距离矩阵。我认为这将需要更多的内存和更多的操作。此外,也不能保证簇的数量低于点的数量。在这个问题上,我只是用了一个玩具的例子。
ClusterIndexes = zeros(npoints,1)
for i = 1:npoints
% sum of squared differences (square root can be avoided)
SquareDist = sum(bsxfun(@minus, Clusters, Points(i,:)).^2, 2);
% Index of the closest cluster
[minVal minIdx] = min(SquareDist);
ClusterIndexes(i) = minIdx;
end
% plot results
figure(2), hold on
plot(Clusters(:,1), Clusters(:,2), 'ro')
plot(Points(:,1), Points(:,2), 'bx')
plot([Points(:,1) Clusters(ClusterIndexes,1)]', [Points(:,2) Clusters(ClusterIndexes,2)]', 'm-');