-
Notifications
You must be signed in to change notification settings - Fork 0
/
findClosestCentroids.m
41 lines (28 loc) · 956 Bytes
/
findClosestCentroids.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
%
% Set K
K = size(centroids, 1);
% return the following variables correctly.
idx = zeros(size(X,1), 1);
% 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
%
for i=1:length(X)
min = inf;
for j=1:K
cDist = norm(X(i,:)-centroids(j,:));
if(cDist < min)
min=cDist;
idx(i)=j;
end
end
end
% =============================================================
end