Find closest centroids
This commit is contained in:
parent
229023b69c
commit
f8c0087ff3
1 changed files with 13 additions and 4 deletions
|
@ -21,11 +21,20 @@ idx = zeros(size(X,1), 1);
|
|||
% Note: You can use a for-loop over the examples to compute this.
|
||||
%
|
||||
|
||||
for i = 1:size(X,1)
|
||||
|
||||
idx(i) = 0; % unassigned yet
|
||||
best_distance = Inf;
|
||||
|
||||
for k = 1:K
|
||||
distance = norm(X(i,:) - centroids(k,:))^2;
|
||||
if distance < best_distance
|
||||
idx(i) = k;
|
||||
best_distance = distance;
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
% =============================================================
|
||||
|
||||
|
|
Reference in a new issue