-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkMeans.m
59 lines (51 loc) · 1.65 KB
/
kMeans.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
% K means clustering
function [assignment, centers]=kMeans(inputData,k)
% kMeans accepts as input a set of m data vectors (as rows), each vector
% has length n. It also accepts k, the number of clusters. It returns a
% vector, assignments, in which assignment(j) gives the cluster number
% to which data vector j is assigned. It also returns the coordinates
% of each cluster center in centers, i.e. center(i,:) gives the center of
% cluster i.
[m,n]=size(inputData);
% Initialize cluster centers
minData=min(inputData);
maxData=max(inputData);
interval=(maxData-minData)/k;
centers=zeros(k,n);
centers_new=centers;
for i=1:k
centers(i,:)=minData+interval/2;
minData=minData+interval;
end
assignment=zeros(m,1);
converged=0;
% iterate until convergence
while (converged~=1)
% Assign each data point to the nearest cluster center
for i=1:m
assignment(i,1)=getClusterCenter(centers,inputData(i,:));
end
% Update cluster centers
for j=1:k
points=find(assignment==j);
if (numel(points)~=0)
centers_new(j,:)=mean(inputData(points,:),1);
end
end
% Check for convergence
if (norm(centers_new-centers)<1)
converged=1;
centers=centers_new;
else
centers=centers_new;
end
end
end
function assignmt=getClusterCenter(centers,datapoint)
% Assigns datapoint to its cluster
k=size(centers,1);
distVectors=centers-repmat(datapoint,k,1);
distances=sum(distVectors.^2,2);
[~,idx]=min(distances);
assignmt=idx;
end