function centroids = computeCentroids(X, idx, K) | |
% Initialize to store new centroid values | |
centroids = zeros(K, n); | |
% iterate over number of centroids | |
for i = 1:K | |
% create a boolean vector that match the current index, then use this to index into X | |
has_this_centroid = X(idx == i, :); | |
% sum up the examples that are assigned this centroid | |
sum_of_assigned = sum(has_this_centroid); | |
% get the number of assigned for easier averaging | |
num_of_assigned = size(has_this_centroid, 1); | |
% calulate and store new centroid | |
centroids(i, :) = (1/num_of_assigned) * sum_of_assigned; | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment