computeCentroids.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function centroids = computeCentroids(X, idx, K)
  2. %COMPUTECENTROIDS returns the new centroids by computing the means of the
  3. %data points assigned to each centroid.
  4. % centroids = COMPUTECENTROIDS(X, idx, K) returns the new centroids by
  5. % computing the means of the data points assigned to each centroid. It is
  6. % given a dataset X where each row is a single data point, a vector
  7. % idx of centroid assignments (i.e. each entry in range [1..K]) for each
  8. % example, and K, the number of centroids. You should return a matrix
  9. % centroids, where each row of centroids is the mean of the data points
  10. % assigned to it.
  11. %
  12. % Useful variables
  13. [m n] = size(X);
  14. % You need to return the following variables correctly.
  15. centroids = zeros(K, n);
  16. % ====================== YOUR CODE HERE ======================
  17. % Instructions: Go over every centroid and compute mean of all points that
  18. % belong to it. Concretely, the row vector centroids(i, :)
  19. % should contain the mean of the data points assigned to
  20. % centroid i.
  21. %
  22. % Note: You can use a for-loop over the centroids to compute this.
  23. %
  24. for k = 1:K
  25. % obtain a vector that contains only k in the idx vector
  26. idx_konly = (idx == k);
  27. % count the number of examples that are assigned centroid k
  28. num_ex_assigned_k = sum(idx_konly);
  29. % compute the mean value for centroid k
  30. % multiplying idx_konly with each element in X results in a matrix that have all the
  31. % unwanted values zeroed out, leaving the ones that are assigned k
  32. centroids(k,:) = 1 / abs(num_ex_assigned_k) * sum(idx_konly .* X);
  33. endfor
  34. % =============================================================
  35. end