I have the following function that works perfectly, but I would like to apply vectorization to it...
for i = 1:size(centroids,1)
centroids(i, :) = mean(X(idx == i, :));
end
It checks if idx
matches the current index and if it does, it calculates the mean
value for all the X
values that correspond to that index.
This is my attempt at vectorization, my solution does not work and I know why...
centroids = mean(X(idx == [1:size(centroids,1)], :));
The following idx == [1:size(centroids,1)]
breaks the code. I have no idea how to check if idx
equals to either of the numbers from 1
to size(centroids,1)
.
tl:dr
Get rid of the for loop through vectorization