Advertisement
Guest User

Untitled

a guest
Mar 30th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 0.76 KB | None | 0 0
  1. % computes a clustering solution total cost
  2. function cost = compute_cost_pc(points, centroids)
  3.     cost = 0;
  4.     % TODO compute clustering solution cost
  5.    [n m] = size(points);
  6.    [a b] = size(centroids);
  7.     poz = zeros(n,1);
  8.     for i = 1:n
  9.       k = 1;
  10.       dist_min = norm(points(i,:) - centroids(1,:));
  11.       for j = 2:a
  12.         dist = norm(points(i,:) - centroids(j,:));
  13.         if (dist < dist_min)
  14.           dist_min = dist;
  15.           k = j;
  16.         endif
  17.       endfor
  18.       poz(i) = k;
  19.     endfor
  20.     val = 1;
  21.     while (val <= a)
  22.       sum = 0;
  23.       for m = 1:n
  24.         if (val == poz(m))
  25.           sum = sum + norm(points(m,:) - centroids(val,:));
  26.         endif
  27.       endfor
  28.       cost = cost + sum;
  29.       val = val + 1;
  30.     endwhile
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement