Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % computes the NC centroids corresponding to the given points using K-Means
- function centroids = clustering_pc(points, NC)
- [n m] = size(points);
- for i = 1:NC
- if i > 1
- y = randi(n);
- if x == y
- y = randi(n);
- endif
- x = y;
- else
- x = randi(n);
- endif
- centroids(i,:) = points(x,:);
- endfor
- prev_centroids = zeros(NC, m);
- error = 0.0001;
- while (abs(prev_centroids - centroids) > error)
- poz = zeros(n,1);
- prev_centroids = centroids;
- for i = 1:n
- k = 1;
- dist_min = norm(points(i,:) - centroids(1,:));
- for j = 2:NC
- dist = norm(points(i,:) - centroids(j,:));
- if (dist < dist_min)
- dist_min = dist;
- k = j;
- endif
- endfor
- poz(i) = k;
- endfor
- val = 1;
- while (val <= NC)
- nr_points = 0;
- sumX = sumY = sumZ = 0;
- for m = 1:n
- if (val == poz(m))
- sumX = sumX + points(m, 1);
- sumY = sumY + points(m, 2);
- sumZ = sumZ + points(m, 3);
- nr_points = nr_points + 1;
- endif
- endfor
- centroids(val, :) = [sumX sumY sumZ] * (1/nr_points);
- val = val + 1;
- endwhile
- endwhile
- % TODO K-Means code
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement