K mean implementation: two implementations, same results, but different - but where is the difference?

Hi! I am supposed to do k-means implementation for a course. And I did it. My problem is: I implemented one part of it in two different ways, both deliver the same results und testsamples but only one passed the (automatical) test. And I have no idea why?
This routine is supposed to calculate the new centroids of the k-means algo. in the following code X is a Matrix of samples, idx is the centroids assignment for every sample for the current centroids and K is the number of centroids.
It starts with some basic values:
if true
[m n] = size(X);
centroids = zeros(K, n);
end
And this is my first and preferential solution:
if true
for i=1:K
XTemp = X(idx==i,:);
centroids(i,:) = mean(XTemp(:,:));
end
end
In the end each and every centroid has some new coordinates.
And this is the ugly implementation that finally did it:
if true
X_sum = zeros();
for i = 1:K
X_sum = 0;
counter = 0;
for j = 1:m
if i == idx(j)
X_sum = X_sum + X(j, :);
counter = counter +1;
end
end
centroids(i, :) = X_sum/counter;
end
end
And now my question: What is the difference? - Because I don't see any!
Thanks.

Answers (1)

mean(XTemp(:,:)) takes mean along the first dimension by default. The loop you programmed is taking the mean along the second dimension. mean(XTemp,2) should have the same effect.

Asked:

on 20 Sep 2017

Commented:

on 20 Sep 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!