Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions Andrew Ng Stanford Coursera/Week 08/ex7/computeCentroids.m
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


% =============================================================


Expand Down