%Ncut谱聚类完整函数定义(保存为.m文件):
function C = SpectralClustering(data,k,a) %data是数据点矩阵 K是聚类个数 a代表高斯核函数的参数
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
d = pdist(data)
d2 = squareform(d)
d3 = d2.^2
W(:,:) = exp(-d3(:,:)/(2*a^2))
[n,m] = size(W);
s = sum(W)
D = full(sparse(1:n,1:n,s))
E = D^(-1/2)*W*D^(-1/2)
[X,B] = eig(E)
[Q,V] = eigs(E,k) %选的是前K个最大特征值对应的特征向量
C = kmeans(Q,k)
end
调用该函数时,首先载入数据,然后给定相关参数后直接调用:
load C:\Users\wangyuanw\Desktop\data\data1new.txt;
SpectralClustering(data1new,3,3);
备注:matlab相关函数功能可用help进行查询