-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNNPCDist.m
39 lines (29 loc) · 903 Bytes
/
NNPCDist.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
% NNPC for precomputed dissimilarity matrix
% D: Dissimilarity matrix
% q: Number of nearest neighbors
% L: Number of clusters
% labels: Labels assigned to the data points
function labels = NNPCDist(D,q,L)
N = size(D,1);
Z = zeros(N,N);
% Compute nearest neighbor graph
for i=1:N
di = D(:,i);
di(i) = inf; % avoid that the ith point is selected
[dio,order] = sort(di, 'ascend');
Z(i, order(1:q) ) = exp(-2*dio(1:q));
end
Z = Z + Z';
% Normalized spectral clustering
Dsum = diag(1./sqrt(sum(Z)+eps));
LN = eye(N) - Dsum*Z*Dsum;
[~,S,V] = svd(LN);
if nargin < 3
singvals = diag(S);
[~,minidx] = min(diff(singvals(1:(end-1))));
L = N - minidx;
end
VL = V(:,N-L+1:N);
VL = normr(VL);
[labels,~] = kmeans(VL,L,'maxiter',1000,'replicates',200,'EmptyAction','singleton');
end