Advertisement
Guest User

Untitled

a guest
Mar 30th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.25 KB | None | 0 0
  1. % computes the NC centroids corresponding to the given points using K-Means
  2. function centroids = clustering_pc(points, NC)
  3.   [n m] = size(points);
  4.   for i = 1:NC
  5.     if i > 1
  6.       y = randi(n);
  7.       if x == y
  8.         y = randi(n);
  9.       endif
  10.       x = y;
  11.     else
  12.       x = randi(n);
  13.     endif
  14.     centroids(i,:) = points(x,:);
  15.   endfor
  16.  
  17.   prev_centroids = zeros(NC, m);
  18.   error = 0.0001;
  19.   while (abs(prev_centroids - centroids) > error)
  20.     poz = zeros(n,1);
  21.     prev_centroids = centroids;
  22.     for i = 1:n
  23.       k = 1;
  24.       dist_min = norm(points(i,:) - centroids(1,:));
  25.       for j = 2:NC
  26.         dist = norm(points(i,:) - centroids(j,:));
  27.         if (dist < dist_min)
  28.           dist_min = dist;
  29.           k = j;
  30.         endif
  31.       endfor
  32.       poz(i) = k;
  33.     endfor
  34.     val = 1;
  35.     while (val <= NC)
  36.       nr_points = 0;
  37.       sumX = sumY = sumZ = 0;
  38.       for m = 1:n
  39.         if (val == poz(m))
  40.           sumX = sumX + points(m, 1);
  41.           sumY = sumY + points(m, 2);
  42.           sumZ = sumZ + points(m, 3);
  43.           nr_points = nr_points + 1;
  44.         endif
  45.       endfor
  46.       centroids(val, :) = [sumX sumY sumZ] * (1/nr_points);
  47.       val = val + 1;
  48.     endwhile
  49.   endwhile
  50.  
  51.     % TODO K-Means code
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement