Commits

Dhananjay Nene committed a97a4b4

Solved parts 1 and 2 of exercise 7

  • Participants
  • Parent commits a8eafcc

Comments (0)

Files changed (2)

File exercise-7/octave/computeCentroids.m

 % Note: You can use a for-loop over the centroids to compute this.
 %
 
+agg = zeros(K,size(X,2));
+cntr = zeros(K,1);
+for r=1:size(X,1)
+  agg(idx(r),:) = agg(idx(r),:) + X(r,:);
+  cntr(idx(r)) = cntr(idx(r)) + 1;
+end
+for r = 1:size(agg,1)
+  centroids(r,:) = agg(r,:) / cntr(r);
+end
+
+
 
 
 

File exercise-7/octave/findClosestCentroids.m

 % Note: You can use a for-loop over the examples to compute this.
 %
 
-
-
-
-
-
+N=[];
+for r=1:size(centroids,1)
+  c = centroids(r,:);
+  N = [N sum((X - ones(size(X,1),1) * c) .^ 2 , 2) ];
+end
+[a,b] = min(N');
+idx = b';
 
 % =============================================================