www.pudn.com > 1_FCM_S.zip > km.m, change:2015-10-18,size:878b


function [centers,assignments,error] = km(data,k,n,t);% k均值聚类
% data is a array with each column a data point in some metric
% space
% k is the number of centers,k为聚类中心数目
% n is the maximum number of iterations n为最大迭代次数
% t is the change threshold

% random initialization

[N1,N2] = size(data);



permutation=randperm(N2);%随机产生N2中的
centers=data(:,permutation(1:k));

change=t+1;
E=sum(data.^2,1)';%data.^2为data的2次幂
Jerr=Inf;
itercount=0;

while (change> t) & (itercount < n);

  F=sum(centers.^2,1)';
  T=E*ones(1,k) + ones(N2,1)*F'-2*data'*centers;
  [mind,minc]=min(T');
  change=Jerr-sum(mind);
  Jerr=sum(mind);

  
  for i = 1:k;
    [I,J]=find(minc==i);
    centers(:,i)=sum(data(:,J),2)/(length(J)+eps);
  end
  itercount=itercount+1;
  
end
F=sum(centers.^2,1)';
T=E*ones(1,k) + ones(N2,1)*F'-2*data'*centers;
[error,assignments]=min(T');