聚类算法matlab程序

Posted 气象又服务

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聚类算法matlab程序相关的知识,希望对你有一定的参考价值。

聚类算法matlab程序


clearall

closeall

disp('Theonlyinputneededisadistancematrixfile')

disp('Theformatofthisfileshouldbe:')

disp('Column1:idofelementi')

disp('Column2:idofelementj')

disp('Column3:dist(i,j)')

mdist=input('example_distances');%nameofthedistancematrixfile(withsinglequotes)?\n

disp('Readinginputdistancematrix')

xx=load(mdist);

ND=max(xx(:,2));

NL=max(xx(:,1));

if(NL>ND)

ND=NL;

end

N=size(xx,1);

fori=1:ND

forj=1:ND

dist(i,j)=0;

end

end

fori=1:N

ii=xx(i,1);

jj=xx(i,2);

dist(ii,jj)=xx(i,3);

dist(jj,ii)=xx(i,3);

end

percent=2.0;

fprintf('averagepercentageofneighbours(hardcoded):%5.6f\n',percent);

position=round(N*percent/100);

sda=sort(xx(:,3));

dc=sda(position);

fprintf('ComputingRhowithgaussiankernelofradius:%12.6f\n',dc);

fori=1:ND

rho(i)=0.;

end

%

%Gaussiankernel

%

fori=1:ND-1

forj=i+1:ND

rho(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));

end

end

%


%"Cutoff"kernel

%

%fori=1:ND-1

%forj=i+1:ND

%if(dist(i,j)<dc)

%rho(i)=rho(i)+1.;

%rho(j)=rho(j)+1.;

%end

%end

%end

maxd=max(max(dist));

[rho_sorted,ordrho]=sort(rho,'descend');

delta(ordrho(1))=-1.;

nneigh(ordrho(1))=0;

forii=2:ND

delta(ordrho(ii))=maxd;

forjj=1:ii-1

if(dist(ordrho(ii),ordrho(jj))<delta(ordrho(ii)))

delta(ordrho(ii))=dist(ordrho(ii),ordrho(jj));

nneigh(ordrho(ii))=ordrho(jj);

end

end

end

delta(ordrho(1))=max(delta(:));

disp('Generatedfile:DECISIONGRAPH')

disp('column1:Density')

disp('column2:Delta')

 

fid=fopen('DECISION_GRAPH','w');

fori=1:ND

fprintf(fid,'%6.2f%6.2f\n',rho(i),delta(i));

end

disp('Selectarectangleenclosingclustercenters')

scrsz=get(0,'ScreenSize');

figure('Position',[672scrsz(3)/4.scrsz(4)/1.3]);

fori=1:ND

ind(i)=i;

gamma(i)=rho(i)*delta(i);

end

subplot(2,1,1)

tt=plot(rho(:),delta(:),'o','MarkerSize',5,'MarkerFaceColor','k','MarkerEdgeColor','k');

title('DecisionGraph','FontSize',15.0)

xlabel('\rho')

ylabel('\delta')


subplot(2,1,1)

rect=getrect(1);

rhomin=rect(1);

deltamin=rect(4);

NCLUST=0;

fori=1:ND

cl(i)=-1;

end

fori=1:ND

if((rho(i)>rhomin)&&(delta(i)>deltamin))

NCLUST=NCLUST+1;

cl(i)=NCLUST;

icl(NCLUST)=i;

end

end

fprintf('NUMBEROFCLUSTERS:%i\n',NCLUST);

disp('Performingassignation')

 

%assignation

fori=1:ND

if(cl(ordrho(i))==-1)

cl(ordrho(i))=cl(nneigh(ordrho(i)));

end

end

%halo

fori=1:ND

halo(i)=cl(i);

end

if(NCLUST>1)

fori=1:NCLUST

bord_rho(i)=0.;

end

fori=1:ND-1

forj=i+1:ND

if((cl(i)~=cl(j))&&(dist(i,j)<=dc))

rho_aver=(rho(i)+rho(j))/2.;

if(rho_aver>bord_rho(cl(i)))

bord_rho(cl(i))=rho_aver;

end

if(rho_aver>bord_rho(cl(j)))

bord_rho(cl(j))=rho_aver;

end

end

end

end

fori=1:ND

if(rho(i)<bord_rho(cl(i)))

halo(i)=0;

end

end

end

fori=1:NCLUST

nc=0;

nh=0;

forj=1:ND

if(cl(j)==i)

nc=nc+1;

end

if(halo(j)==i)

nh=nh+1;

end

end

fprintf('CLUSTER:%iCENTER:%iELEMENTS:%iCORE:%iHALO:%i\n',i,icl(i),nc,nh,nc-nh);

end

 

cmap=colormap;

fori=1:NCLUST

ic=int8((i*64.)/(NCLUST*1.));

subplot(2,1,1)

holdon

 

plot(rho(icl(i)),delta(icl(i)),'o','MarkerSize',8,'MarkerFaceColor',cmap(ic,:),'MarkerEdgeColor',cmap(ic,:));

end

subplot(2,1,2)

disp('Performing2Dnonclassicalmultidimensionalscaling')

Y1=mdscale(dist,2,'criterion','metricstress');

plot(Y1(:,1),Y1(:,2),'o','MarkerSize',2,'MarkerFaceColor','k','MarkerEdgeColor','k');

title('2DNonclassicalmultidimensionalscaling','FontSize',15.0)

xlabel('X')

ylabel('Y')

fori=1:ND

A(i,1)=0.;

A(i,2)=0.;

end

fori=1:NCLUST

nn=0;

ic=int8((i*64.)/(NCLUST*1.));

forj=1:ND

if(halo(j)==i)

nn=nn+1;

A(nn,1)=Y1(j,1);

A(nn,2)=Y1(j,2);

end

end

holdon

plot(A(1:nn,1),A(1:nn,2),'o','MarkerSize',2,'MarkerFaceColor',cmap(ic,:),'MarkerEdgeColor',cmap(ic,:));

end

 

%fori=1:ND

%if(halo(i)>0)

%ic=int8((halo(i)*64.)/(NCLUST*1.));

%holdon

%

plot(Y1(i,1),Y1(i,2),'o','MarkerSize',2,'MarkerFaceColor',cmap(ic,:),'MarkerEdgeColor',cmap(ic,:));

%end

%end

faa=fopen('CLUSTER_ASSIGNATION','w');

disp('Generatedfile:CLUSTER_ASSIGNATION')

disp('column1:elementid')

disp('column2:clusterassignationwithouthalocontrol')

disp('column3:clusterassignationwithhalocontrol')

fori=1:ND

fprintf(faa,'%i%i%i\n',i,cl(i),halo(i));

end

 

 


以上是关于聚类算法matlab程序的主要内容,如果未能解决你的问题,请参考以下文章

基于MATLAB的Kmeans聚类算法的仿真与分析

聚类算法及Matlab实现

数据聚类基于matlab杂草算法优化K-means算法数据聚类含Matlab源码 2168期

数学建模MATLAB应用实战系列(106)-机器学习算法:K-means聚类(附MATLAB代码)

图像分割基于matlab遗传算法优化K聚类图像分割含Matlab源码 1605期

K-medodis聚类算法MATLAB