-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchors.m
52 lines (44 loc) · 1.36 KB
/
anchors.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
function [centers,index] = anchors(data,k)
%function[centers, mincenter, mindist, lower, computed] = anchors(firstcenter,k,data)
% choose k centers by the furthest-first method
[n,dim] = size(data);
firstcenterIndex=randi(n);
centers = zeros(k,dim);
index=zeros(k,1);
lower = zeros(n,k);
mindist = Inf*ones(n,1);
mincenter = ones(n,1);
computed = 0;
centdist = zeros(k,k);
firstcenter=data(firstcenterIndex,:);
index(1)=firstcenterIndex;
for j = 1:k
if j == 0
newcenter = firstcenter;
else
[~,i] = max(mindist);
newcenter = data(i,:);
index(j)=i;
end
centers(j,:) = newcenter;
centdist(1:j-1,j) = calcdist(centers(1:j-1,:),newcenter);
centdist(j,1:j-1) = centdist(1:j-1,j)';
computed = computed + j-1;
inplay = find(mindist > centdist(mincenter,j)/2);
newdist = calcdist(data(inplay,:),newcenter);
computed = computed + size(inplay,1);
lower(inplay,j) = newdist;
% other = find(mindist <= centdist(mincenter,j)/2);
% if ~isempty(other)
% lower(other,j) = centdist(mincenter(other),j) - mindist(other);
% end
move = find(newdist < mindist(inplay));
shift = inplay(move);
mincenter(shift) = j;
mindist(shift) = newdist(move);
end
%udist = calcdist(data,centers(mincenter,:));
%quality = mean(udist);
%q2 = mean(udist.^2);
%[k toc quality q2 computed]
%toc