Trouble with for loop, closest centroid only showing up for first training example

29 Views Asked by At

Im currently doing a Machine Learning course and Im having a little problem with my code. I am creating a function that will return the Index of the Cluster centroid closest to training example X, Where initial_centroids = [3 3; 6 2; 8 5]; and X 1-3 = [1.8421,4.6076;5.6586,4.8;6.3526,3.2909]

My idea was to make a for loop, calculating the distances between X1 and centroid 1/2/3, and then pick the lowest one. Returning the Index of which was the lowest.

When Calling the function, my answer (provided) should be [1 3 2].

What I receive is [1 0 0] .

I believe my for loop has a mistake, because idx was previously set as a vector of 0's and now just the first 0 has been changed to a 1. Suggesting that my for loop only works for the first i?

I cannot seem to spot the mistake, Im quite new to programming, and I dont want to look at ready solutions for these excersises, so I was hoping someone would at least guide me in the right direction, no need for a direct solution.

function idx = findClosestCentroids(X, centroids)
%FINDCLOSESTCENTROIDS computes the centroid memberships for every example
%   idx = FINDCLOSESTCENTROIDS (X, centroids) returns the closest centroids
%   in idx for a dataset X where each row is a single example. idx = m x 1 
%   vector of centroid assignments (i.e. each entry in range [1..K])
%

% Set K
K = size(centroids, 1);
tempidx = zeros(1,2);

% You need to return the following variables correctly.
idx = zeros(size(X,1), 1);
for i = 1:X;
    C = [(X(i,:)-centroids(1,:)).^2;(X(i,:)-centroids(2,:)).^2;(X(i,:)-centroids(3,:)).^2];
    Ctot = sum(C(i),2);
    [res,dx] = min(Ctot);
    idx(i,:) = dx;
end;
% ====================== YOUR CODE HERE ======================
% Instructions: Go over every example, find its closest centroid, and store
%               the index inside idx at the appropriate location.
%               Concretely, idx(i) should contain the index of the centroid
%               closest to example i. Hence, it should be a value in the 
%               range 1..K
%
% Note: You can use a for-loop over the examples to compute this.
%



The lines that call function and return [1 0 0] (should be [1 3 2] are:
idx = findClosestCentroids(X, initial_centroids);
fprintf('Closest centroids for the first 3 examples: %d %d %d', idx(1:3))




What I tried to change:

for i = 1:X;
    C = [(X(i,:)-centroids(1,:)).^2;(X(i,:)-centroids(2,:)).^2;(X(i,:)-centroids(3,:)).^2];
    Ctot(i) = sum(C(i),2);
    [res,dx] = min(Ctot(i));
    idx(i,:) = dx(i);
end;

Still didnt work and returned [1 0 0]....

I thought perhaps I was not referring to the iteration somehow..

1

There are 1 best solutions below

0
MattStr On

FOUND AN ANSWER MYSELF. (very proud)

Was about to log off. Then I saw my for loop is set up: For i = 1:X.

This will cause the loop to stop at 1, a fix is setting up the for loop:

For i = 1:size(X,1)

My working solution:

idx = zeros(size(X,1), 1);
for i = 1:size(X,1);
C = [(X(i,:)-centroids(1,:)).^2;(X(i,:)-centroids(2,:)).^2;(X(i,:)-centroids(3,:)).^2];

Ctot = sum(C,2);

[res,dx] = min(Ctot);

idx(i,:) = dx;
end;