Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. function centroids = clustering_pc(points, NC)
  2. % TODO K-Means code
  3. [m n] = size(points);
  4. clusters = zeros(NC,n);
  5. %step 1
  6. for i = 1 : NC
  7. for j = 1 : n
  8. idx = randi(m);
  9. temp(i,:) = centroids(i,:) = points(randi(m),:);
  10. endfor
  11. endfor
  12. dist = zeros(m,1);
  13. n = 0
  14. %step 2
  15. for iter = 1 : 1000
  16. for i = 1: m
  17. for j = 1: n
  18. for k = 1: NC
  19. dist(k,:) = norm(centroids(k,:) - points(i,:));
  20. endfor
  21. [index] = min(min(dist));
  22. for k = 1: NC
  23. if (k == index)
  24. clusters(k,:) = points(i,:) + clusters(i,:);
  25.  
  26. endif
  27. centroids(k,:) = mean(clusters);
  28. endfor
  29. endfor
  30. endfor
  31. if temp != centroids
  32. temp = centroids;
  33. else
  34. break;
  35. endif
  36. endfor
  37.  
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement