Advertisement
Guest User

Untitled

a guest
Mar 30th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.06 KB | None | 0 0
  1. % outputs a graphical representation of the clustering solution
  2. function view_clusters(points, centroids)
  3.     % TODO graphical representation coded here
  4.  
  5.   % number of points
  6.   NP = rows(points);
  7.   % number of clusters
  8.   NC = rows(centroids);
  9.  
  10.   % function that leads the interval (0, 1) in the interval ((i - 1) / NC, i / NC)
  11.   get_color = @(i) (rand(1 ,1) + i - 1) / NC;
  12.   color = arrayfun(get_color, [randperm(NC); randperm(NC); randperm(NC)]);
  13.  
  14.   if NC == 1
  15.     scatter3(points(:, 3), points(:, 2), points(:, 1),
  16.              [10], [rand(1, 1) rand(1, 1) rand(1, 1)], 'filled');
  17.     hold on;
  18.     return
  19.   endif
  20.  
  21.   % function that get for each column of x the row where is the min
  22.   f = @(x) [i j] = find(x == min(x));
  23.  
  24.   % the cluster of each point
  25.   cluster = f(reshape(norm(repmat(centroids, NP, 1) - kron(points, ones(NC, 1)), 'rows'), NC, NP));
  26.  
  27.   for i = 1 : NC
  28.     v = points(find(cluster == i), :);
  29.  
  30.     scatter3(v(:, 3), v(:, 2), v(:, 1),
  31.              [10], [color(i, 1) color(i, 2) color(i, 3)], 'filled');
  32.     hold on;
  33.   end
  34.  
  35. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement