资源描述:
《聚类算法matlab程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、clearallclosealldisp('Theonlyinputneededisadistancematrixfile')disp('Theformatofthisfileshouldbe:')disp('Column1:idofelementi')disp('Column2:idofelementj')disp('Column3:dist(i,j)')mdist=input('example_distances');%nameofthedistancematrixfile(withsinglequotes)?disp('Readinginputdistancema
2、trix')xx=load(mdist);ND=max(xx(:,2));NL=max(xx(:,1));if(NL>ND)ND=NL;endN=size(xx,1);fori=1:NDforj=1:NDdist(i,j)=0;endendfori=1:Nii=xx(i,1);jj=xx(i,2);dist(ii,jj)=xx(i,3);dist(jj,ii)=xx(i,3);endpercent=2.0;fprintf('averagepercentageofneighbours(hardcoded):%5.6f',percent);position=round(N*
3、percent/100);sda=sort(xx(:,3));dc=sda(position);fprintf('ComputingRhowithgaussiankernelofradius:%12.6f',dc);fori=1:NDrho(i)=0.;end%%Gaussiankernel%fori=1:ND-1forj=i+1:NDrho(i)=rho(i)+exp(-(dist(i,j)/dc)*(dist(i,j)/dc));rho(j)=rho(j)+exp(-(dist(i,j)/dc)*(dist(i,j)/dc));endend%%"Cutoff"ker
4、nel%%fori=1:ND-1%forj=i+1:ND%if(dist(i,j)5、lta(ordrho(ii))=dist(ordrho(ii),ordrho(jj));nneigh(ordrho(ii))=ordrho(jj);endendenddelta(ordrho(1))=max(delta(:));disp('Generatedfile:DECISIONGRAPH')disp('column1:Density')disp('column2:Delta')fid=fopen('DECISION_GRAPH','w');fori=1:NDfprintf(fid,'%6.2f%6.2f',rho(i),delta(i));enddisp('Sel
6、ectarectangleenclosingclustercenters')scrsz=get(0,'ScreenSize');figure('Position',[672scrsz(3)/4.scrsz(4)/1.3]);fori=1:NDind(i)=i;gamma(i)=rho(i)*delta(i);endsubplot(2,1,1)tt=plot(rho(:),delta(:),'o','MarkerSize',5,'MarkerFaceColor','k','MarkerEdgeColor','k');title('DecisionGraph','FontSiz
7、e',15.0)xlabel('rho')ylabel('delta')subplot(2,1,1)rect=getrect(1);rhomin=rect(1);deltamin=rect(4);NCLUST=0;fori=1:NDcl(i)=-1;endfori=1:NDif((rho(i)>rhomin)&&(delta(i)>deltamin))NCLUST=NCLUST+1;cl(i)=NCLUST;icl(NCLUST)=i;endendfprintf('NUMBEROFCLUSTERS:%i',