您需要在使用hypo的地方定义一个距离函数。通常用K表示
距离=和((点形心)^2)
这里有一些matlab代码可以做到。。。如果你不能的话,我可以把它移走,但是试试看。就像你说的,学习的唯一途径。在function idx = findClosestCentroids(X, centroids)
%FINDCLOSESTCENTROIDS computes the centroid memberships for every example
% idx = FINDCLOSESTCENTROIDS (X, centroids) returns the closest centroids
% in idx for a dataset X where each row is a single example. idx = m x 1
% vector of centroid assignments (i.e. each entry in range [1..K])
%
% Set K
K = size(centroids, 1);
[numberOfExamples numberOfDimensions] = size(X);
% You need to return the following variables correctly.
idx = zeros(size(X,1), 1);
% Go over every example, find its closest centroid, and store
% the index inside idx at the appropriate location.
% Concretely, idx(i) should contain the index of the centroid
% closest to example i. Hence, it should be a value in the
% range 1..K
%
for loop=1:numberOfExamples
Distance = sum(bsxfun(@minus,X(loop,:),centroids).^2,2);
[value index] = min(Distance);
idx(loop) = index;
end;
end
更新
这应该返回距离,注意上面的matlab代码只返回最近质心的距离(和索引)…您的函数返回所有距离,下面的函数也是这样。在
^{pr2}$