Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function centroids = clustering_pc(points, NC)
- % TODO K-Means code
- [m n] = size(points);
- clusters = zeros(NC,n);
- %step 1
- for i = 1 : NC
- for j = 1 : n
- idx = randi(m);
- temp(i,:) = centroids(i,:) = points(randi(m),:);
- endfor
- endfor
- dist = zeros(m,1);
- n = 0
- %step 2
- for iter = 1 : 1000
- for i = 1: m
- for j = 1: n
- for k = 1: NC
- dist(k,:) = norm(centroids(k,:) - points(i,:));
- endfor
- [index] = min(min(dist));
- for k = 1: NC
- if (k == index)
- clusters(k,:) = points(i,:) + clusters(i,:);
- endif
- centroids(k,:) = mean(clusters);
- endfor
- endfor
- endfor
- if temp != centroids
- temp = centroids;
- else
- break;
- endif
- endfor
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement