kmeans.m

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· M 代码 · 共 37 行

M
37
字号
function [io,c,nx] = kmeans(x,nc)
% KMEANS : k-means clustering
% c = kmeans(x,nc)
%	x       - d*n samples
%	nc      - number of clusters wanted
%	c       - calculated membership vector
% algorithm taken from Sing-Tze Bow, 'Pattern Recognition'

% Copyright (c) 1995 Frank Dellaert
% All rights Reserved

[d,n] = size(x);

%------------------------------------------------------------------------
% step 1: Arbitrarily choose nc samples as the initial cluster centers
%------------------------------------------------------------------------
ir=randperm(d);
ir=ir(1:nc)';
c=x(ir,:);
%[indAng,distAng]=knn(VCP(:,1:NCP),ERACP(:,1:NCP),ParamPrdc.NumA*2,'Norm-2');
io=NaN*ones([d,1]);
moved=d;
while(moved~=0)
   [in]=MLknn(x,c,1,'Norm-2');
   for i=1:nc
      ix=find(in==i);
      nx(i,1)=size(ix,1);
      c(i,:)=nanmean(x(ix,:),1);
   end
   moved=sum(io~=in,1);
   io=in;
   disp(['moved = ' num2str(moved)]);
end



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?