|
|
|
@ -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
|
|
|
|
|
|
|
|
|
|
% =============================================================
|
|
|
|
|
|
|
|
|
|