diff --git a/Andrew Ng Stanford Coursera/Week 08/ex7/computeCentroids.m b/Andrew Ng Stanford Coursera/Week 08/ex7/computeCentroids.m index e330da1..e347ff7 100644 --- a/Andrew Ng Stanford Coursera/Week 08/ex7/computeCentroids.m +++ b/Andrew Ng Stanford Coursera/Week 08/ex7/computeCentroids.m @@ -1,5 +1,5 @@ function centroids = computeCentroids(X, idx, K) -%COMPUTECENTROIDS returs the new centroids by computing the means of the +%COMPUTECENTROIDS returns the new centroids by computing the means of the %data points assigned to each centroid. % centroids = COMPUTECENTROIDS(X, idx, K) returns the new centroids by % computing the means of the data points assigned to each centroid. It is @@ -26,16 +26,20 @@ % Note: You can use a for-loop over the centroids to compute this. % -counts = zeros(K,1); -for i = 1:m, - for j = 1:K, - if idx(i) == j, - centroids(j,:) = (centroids(j,:) + X(i,:)); - counts(j) = counts(j) + 1; +counts=zeros(K,1) +for i = 1:m + for j =1:K + if idx(i)==j + centroids(j,:)=centroids(j,:)+X(i,:) + counts(j)=counts(j)+1 + end end - end -end -centroids = centroids./counts; +end +for k=1:n + centroids(:,k)=centroids(:,k)./counts +end + + % =============================================================