-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitCentroids.m
56 lines (42 loc) · 1.43 KB
/
initCentroids.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
function C=initCentroids(X,k, method)
%INITCENTROIDS Initialice centroids for k-means in a deterministic way.
% C = initCentroids(X,k) produces a 1-by-k matrix containing several centroids
if nargin < 2
error(message('initCentroids:TooFewInputs'));
end
if nargin < 3
method = 'extremevalues';
end
switch(method)
case {'maxvariance'}
% TODO: Finish this method
% unnecesary
% var=sqrt(std(X)
% sort by variance/std
[y,idx]=sort(std(X));
% get the highest variance
[y,idx] = sort(X(:,idx(1)),'descend');
clear y;
C = X(idx(1:k),:);
case {'extremevalues'}
% get the highest range (No funciona si normalizamos)
%[y,idx]=max(max(X)-min(X));
% get the highest standard deviation variable
[y,idx]=max(std(X));
[y,idx] = max(X(:,idx(1)));
clear y;
% First centroid
C(1,:) = X(idx,:);
% figure;hold on;
% plot(X(:,1),X(:,2),'.','markersize',5)
% plot(C(:,1),C(:,2),'r*','markersize',10)
for i=2:k
% Original (Javi)
%[y,idx] = max(sum(pdist2(X,C),2));
distances = pdist2(X,C);
[y,idx] = max(sum(distances,2).*all(distances,2));
C(i,:) = X(idx,:);
% plot(C(:,1),C(:,2),'r*','markersize',10)
end
% hold off;
end