算法流程图开始读入要分类的数据设置初始聚类中心计算数据到C个聚类中心的距离将数据分入与其距离最小的聚类计算新的聚类中心聚类中心是否收敛?否输出C个分类好的聚类结束是三、实验源代码1、主程序clearallclc[FHFW]=textread('C:\Users\lenvo\Desktop\н¨Îļþ¼Ð\FEMALE.txt','%f%f');[MHMW]=textread('C:\Users\lenvo\Desktop\н¨Îļþ¼Ð\MALE.txt','%f%f');Data(1:50,1)=FH;Data(51:100,1)=MH;Data(1:50,2)=FW;Data(51:100,2)=MW;C=input('ÊäÈëC£º')[U,P,Dist,Cluster_Res,Obj_Fcn,iter]=fuzzycm(Data,C)plot(Data(:,1),Data(:,2),'o');holdon;maxU=max(U);index1=find(U(1,:)==maxU);index2=find(U(2,:)==maxU);line(Data(index1,1),Data(index1,2),'marker','*','color','g');line(Data(index2,1),Data(index2,2),'marker','*','color','r');plot([P([12],1)],[P([12],2)],'*','color','k')holdoff;2、子程序function[U,P,Dist,Cluster_Res,Obj_Fcn,iter]=fuzzycm(Data,C,plotflag,M,epsm)ifnargin5epsm=1.0e-6;endifnargin4M=2;endifnargin3plotflag=0;end[N,S]=size(Data);m=2/(M-1);iter=0;Dist(C,N)=0;U(C,N)=0;P(C,S)=0;U0=rand(C,N);U0=U0./(ones(C,1)*sum(U0));¨whiletrueiter=iter+1;Um=U0.^M;P=Um*Data./(ones(S,1)*sum(Um'))';fori=1:Cforj=1:NDist(i,j)=fuzzydist(P(i,:),Data(j,:));endendU=1./(Dist.^m.*(ones(C,1)*sum(Dist.^(-m))));ifnargout4|plotflagObj_Fcn(iter)=sum(sum(Um.*Dist.^2));endifnorm(U-U0,Inf)epsmbreakendU0=U;endifnargout3res=maxrowf(U);forc=1:Cv=find(res==c);Cluster_Res(c,1:length(v))=v;endendifplotflagfcmplot(Data,U,P,Obj_Fcn);endfunction[U,P,Dist,Cluster_Res,Obj_Fcn,iter]=fuzzycm2(Data,P0,plotflag,M,epsm)ifnargin5epsm=1.0e-6;endifnargin4M=2;endifnargin3plotflag=0;end[N,S]=size(Data);m=2/(M-1);iter=0;C=size(P0,1);Dist(C,N)=0;U(C,N)=0;P(C,S)=0;whiletrueiter=iter+1;fori=1:Cforj=1:NDist(i,j)=fuzzydist(P0(i,:),Data(j,:));endendU=1./(Dist.^m.*(ones(C,1)*sum(Dist.^(-m))));Um=U.^M;P=Um*Data./(ones(S,1)*sum(Um'))';ifnargout4|plotflagObj_Fcn(iter)=sum(sum(Um.*Dist.^2));endifnorm(P-P0,Inf)epsmbreakendP0=P;endifnargout3res=maxrowf(U);forc=1:Cv=find(res==c);Cluster_Res(c,1:length(v))=v;endendifplotflagfcmplot(Data,U,P,Obj_Fcn);endfunctionf=addr(a,strsort)ifnargin==1strsort='ascend';endsa=sort(a);ca=a;la=length(a);f(la)=0;fori=1:laf(i)=find(ca==sa(i),1);ca(f(i))=NaN;endifstrcmp(strsort,'descend')f=fliplr(f);endfunctionellipse(a,b,center,style,c_3d)ifnargin4style='b';endifnargin3|isempty(center)center=[0,0];endt=1:360;x=a/2*cosd(t)+center(1);y=b/2*sind(t)+center(2);ifnargin4plot3(x,y,ones(1,360)*c_3d,style)elseplot(x,y,style)endfunctionfcmplot(Data,U,P,Obj_Fcn)[C,S]=size(P);res=maxrowf(U);str='po*x+d^v.h';figure(1),plot(Obj_Fcn)title('Ä¿±êº¯ÊýÖµ±ä»¯ÇúÏß','fontsize',8)ifS==2figure(2),plot(P(:,1),P(:,2),'rs'),holdonfori=1:Cv=Data(find(res==i),:);plot(v(:,1),v(:,2),str(rem(i,12)+1))ellipse(max(v(:,1))-min(v(:,1)),...max(v(:,2))-min(v(:,2)),...[max(v(:,1))+min(v(:,1)),...max(v(:,2))+min(v(:,2))]/2,'r:')endgridon,title('2D¾ÛÀà½á¹ûͼ','fontsize',8),holdoffendifS2figure(2),plot3(P(:,1),P(:,2),P(:,3),'rs'),holdonfori=1:Cv=Data(find(res==i),:);plot3(v(:,1),v(:,2),v(:,3),str(rem(i,12)+1))ellipse(max(v(:,1))-min(v(:,1)),...max(v(:,2))-min(v(:,2)),...[max(v(:,1))+min(v(:,1)),...max(v(:,2))+min(v(:,2))]/2,...'r:',(max(v(:,3))+min(v(:,3)))/2)endgridon,title('3D¾ÛÀà½á¹ûͼ','fontsize',8),holdoffendfunctionD=fuzzydist(A,B)D=norm(A-B);functionmr=maxrowf(U,c)ifnargin2c=1;endN=size(U,2);mr(1,N)=0;forj=1:Naj=addr(U(:,j),'descend');mr(j)=aj(c);end四、实验结果1、FEMALE和MALE(1)C=2,Z1(1)=(173,53)T,Z2(1)=(168,57)T。聚类中心为(163.322052.5232),(175.256567.6907)迭代次数为28(2)C=2,Z1(1)=(173,53)T,Z2(1)=(160,58)T。聚类中心为(163.322052.5232),(175.256567.6907)迭代次数为28(3)C=3,Z1(1)=(173,53)T,Z2(1)=(168,57)TZ3(1)=(160,58)T聚类中心为(168.413957.3300)(176.335169.4859)(160.176749.1940)迭代次数为41(4)C=3,Z1(1)=(173,53)T,Z2(1)=(168,57)TZ3(1)=(161,45)T聚类中心为(168.413957.3300)(176.335169.4859)(160.176749.1940)迭代次数为412、test2(1)C=2,Z1(1)=(173,53)T,Z2(1)=(168,57)T。聚类中心为(166.844354.9270),(176.677270.7966)迭代次数为53(2)C=2,Z1(1)=(173,53)T,Z2(1)=(160,58)T。聚类中心为(166.844354.9270),(176.677270.7966)迭代次数为53(3)C=3,Z1(1)=(173,53)T,Z2(1)=(168,57)TZ3(1)=(160,58)T聚类中心为(178.869980.0977)(163.946951.6216)(174.814464.8963)迭代次数为56(4)C=3,Z1(1)=(173,53)T,Z2(1)=(168,57)TZ3(1)=(161,45)T聚类中心为(178.869980.0977)(163.946951.6216)(174.814464.8963)迭代次数为56结论(一):初始均值设置的不同会影响迭代的次数以及各次迭代所产生的聚类中心,但它不会影响最后的分类结果。结论(二):数据的输入顺序不同,同样影响迭代次数,而对聚类结果没有太大的影响。