|
|
@ -19,18 +19,27 @@ centroids = zeros(K, n);
|
|
|
|
|
|
|
|
|
|
|
|
% ====================== YOUR CODE HERE ======================
|
|
|
|
% ====================== YOUR CODE HERE ======================
|
|
|
|
% Instructions: Go over every centroid and compute mean of all points that
|
|
|
|
% Instructions: Go over every centroid and compute mean of all points that
|
|
|
|
% belong to it. Concretely, the row vector centroids(i, :)
|
|
|
|
% belong to it. Concretely, the row vector centroids(k, :)
|
|
|
|
% should contain the mean of the data points assigned to
|
|
|
|
% should contain the mean of the data points assigned to
|
|
|
|
% centroid i.
|
|
|
|
% centroid k.
|
|
|
|
%
|
|
|
|
%
|
|
|
|
% Note: You can use a for-loop over the centroids to compute this.
|
|
|
|
% Note: You can use a for-loop over the centroids to compute this.
|
|
|
|
%
|
|
|
|
%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for k = 1:K
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
% XXX vectorize
|
|
|
|
|
|
|
|
for i = 1:m
|
|
|
|
|
|
|
|
if idx(i) == k
|
|
|
|
|
|
|
|
centroids(k, :) += X(i, :);
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if count > 0
|
|
|
|
|
|
|
|
centroids(k, :) /= count;
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
% =============================================================
|
|
|
|
% =============================================================
|
|
|
|