-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0c964e
commit c68aa6a
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
%-------------------------------------------------------------------------- | ||
% This function takes an adjacency matrix of a graph and computes the | ||
% clustering of the nodes using the spectral clustering algorithm of | ||
% Ng, Jordan and Weiss. | ||
% CMat: NxN adjacency matrix | ||
% n: number of groups for clustering | ||
% groups: N-dimensional vector containing the memberships of the N points | ||
% to the n groups obtained by spectral clustering | ||
%-------------------------------------------------------------------------- | ||
% Copyright @ Ehsan Elhamifar, 2012 | ||
%-------------------------------------------------------------------------- | ||
|
||
function groups = SpectralClustering(CKSym,n) | ||
|
||
warning off; | ||
N = size(CKSym,1); | ||
MAXiter = 1000; % Maximum number of iterations for KMeans | ||
REPlic = 100; % Number of replications for KMeans | ||
|
||
% Normalized spectral clustering according to Ng & Jordan & Weiss | ||
% using Normalized Symmetric Laplacian L = I - D^{-1/2} W D^{-1/2} | ||
|
||
DN = diag( 1./sqrt(sum(CKSym)+eps) ); | ||
LapN = speye(N) - DN * CKSym * DN; | ||
[uN,sN,vN] = svd(LapN); | ||
kerN = vN(:,N-n+1:N); | ||
for i = 1:N | ||
kerNS(i,:) = kerN(i,:) ./ norm(kerN(i,:)+eps); | ||
end | ||
groups = kmeans(kerNS,n,'maxiter',MAXiter,'replicates',REPlic,'EmptyAction','singleton'); |