diff --git a/ex7/computeCentroids.m b/ex7/computeCentroids.m index 2bd6095..5cadc20 100644 --- a/ex7/computeCentroids.m +++ b/ex7/computeCentroids.m @@ -27,17 +27,9 @@ centroids = zeros(K, n); % for k = 1:K - count = 0; - - % XXX vectorize - for i = 1:m - if idx(i) == k - centroids(k, :) += X(i, :); - count += 1; - end - end - if count > 0 - centroids(k, :) /= count; + X_in_k = X(idx == k, :); + if size(X_in_k, 1) > 0 + centroids(k, :) = sum(X_in_k)/size(X_in_k, 1); end end