Skip to content

Instantly share code, notes, and snippets.

@theptrk
Created April 20, 2020 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theptrk/875379cc34de64925007ec99831641df to your computer and use it in GitHub Desktop.
Save theptrk/875379cc34de64925007ec99831641df to your computer and use it in GitHub Desktop.
function centroids = kMeansInitCentroids(X, K)
% initialize placeholder
centroids = zeros(K, size(X, 2));
% use randperm to shuffle the row indices of X
shuffle_X = randperm(size(X,1));
% only use the first K number of shuffled indices
first_K = shuffle_X(1:K, :);
for i=1:K
centroids(i, :) = X(i, :);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment