kMeansInitCentroids.m 798 B

123456789101112131415161718192021222324252627282930
  1. function centroids = kMeansInitCentroids(X, K)
  2. %KMEANSINITCENTROIDS This function initializes K centroids that are to be
  3. %used in K-Means on the dataset X
  4. % centroids = KMEANSINITCENTROIDS(X, K) returns K initial centroids to be
  5. % used with the K-Means on the dataset X
  6. %
  7. % You should return this values correctly
  8. centroids = zeros(K, size(X, 2));
  9. % ====================== YOUR CODE HERE ======================
  10. % Instructions: You should set centroids to randomly chosen examples from
  11. % the dataset X
  12. %
  13. % Initialize the centroids to be random examples
  14. % Randomly reorder the indices of examples
  15. randidx = randperm(size(X, 1));
  16. % Take the first K examples as centroids
  17. centroids = X(randidx(1:K), :);
  18. % =============================================================
  19. end