1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| function [fin cntrf]=kmoyenne(M,c)
[nl nc]=size(M);fin=zeros(nl,nc);
Ml=reshape(double(M),1,[]);
p=max(Ml)/c;
oldcentr=(p/2:p:max(Ml));
Z=distcntr(Ml,oldcentr,c)';
out=classe(Z',c);
cntr=newcntr(Ml,out',c);
while (max(abs(oldcentr-cntr))>0)
oldcentr=cntr;
Z=distcntr(Ml,oldcentr,c)';
out=classe(Z',c);
cntr=newcntr(Ml,out',c);
end
out=reshape(out,nl,nc);
[cntrf Nindx ]=sort(cntr);
for i=1:c
%M=zeoros(nl,nc);
R(:,:)=out==Nindx(i);
fin(R)=i;
end
function out=distcntr(Ml,oldcentr,c)
J=Ml;
for i=1:c
J(i+1,:)=abs(Ml-oldcentr(1,i));
end
out=J;
function out=classe(J,c)
T=min(J(2:c+1,:));
for i=1:c
Z(i,:)=(J(i+1,:)==T)*i;
end
out=sum(Z);
function out=newcntr(Ml,rgn,c)
for i=1:c
indx=rgn==i;
Nbr=sum(indx);
out(i)=sum(Ml(indx))/Nbr;
end |
Partager