-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalcImpNNs.m
executable file
·52 lines (44 loc) · 1 KB
/
calcImpNNs.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
function impNNs = calcImpNNs(x, y, numclasses, k2)
% x: each row is a data point
x = x';
impNNs = [];
fprintf('Computing imposter nearest neighbors ...\n');
[D,N]=size(x);
un=unique(y);
Gnn=zeros(k2,N);
if length(un)~=numclasses
error('incorrect label inputs or numclasses');
end
for ii=1:numclasses-1
for c=1:length(un)
i=find(y==c);
c2= mod((c - 1 + ii),numclasses) + 1;
i2=find(y==c2);
fprintf('%i nearest imposter neighbors for class %i with imposter class %i:',k2, c, c2);
nn=LSKnn(x(:,i2),x(:,i),1:k2);
Gnn(:,i)=i2(nn);
fprintf('\n');
end
NNs = Gnn';
impNNs = [impNNs NNs];
end
function NN=LSKnn(X1, X2,ks);
B=3000;
%ks=[1:k]
[D,N]=size(X2);
NN=zeros(length(ks),N);
DD=zeros(length(ks),N);
for i=1:B:N
BB=min(B,N-i);
fprintf('.');
Dist=distance(X1,X2(:,i:i+BB));
fprintf('.');
[dist,nn]=mink(Dist,length(ks)+1);
clear('Dist');
fprintf('.');
% keyboard;
% note here X1 doesnt intersect X2
NN(:,i:i+BB)=nn(ks,:);
clear('nn','dist');
fprintf('(%i%%) ',round((i+BB)/N*100));
end;