diff --git a/Week 8/Programming Assignment/machine-learning-ex7/ex7/findClosestCentroids.m b/Week 8/Programming Assignment/machine-learning-ex7/ex7/findClosestCentroids.m index fb89ab1..54ceb83 100644 --- a/Week 8/Programming Assignment/machine-learning-ex7/ex7/findClosestCentroids.m +++ b/Week 8/Programming Assignment/machine-learning-ex7/ex7/findClosestCentroids.m @@ -21,16 +21,18 @@ % Note: You can use a for-loop over the examples to compute this. % -m = size(X,1); - -for i = 1:m - distance_array = zeros(1,K); - for j = 1:K - distance_array(1,j) = sqrt(sum(power((X(i,:)-centroids(j,:)),2))); - end - [~, d_idx] = min(distance_array); - idx(i,1) = d_idx; -end +[k, n] = size(centroids); +[m, n] = size(X); + +X = reshape(X, m, 1, n); +centroids = reshape(centroids, 1, k, n); + +dist = (X - centroids) .^ 2; +dist = sum(dist, 3); + +[_ idx] = min(dist'); + +idx = idx'; % =============================================================