Advertisement
Guest User

Untitled

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