File Exchange - MATLAB Central
File Exchange - MATLAB Central
function[center,U,obj_fcn]=fcm(data,cluster_n,options)
%FCMDatasetclusteringusingfuzzycmeansclustering.
%
%[CENTER,U,OBJ_FCN]=FCM(DATA,N_CLUSTER)findsN_CLUSTERnumberof
%clustersinthedatasetDATA.DATAissizeMbyN,whereMisthenumberof
%datapointsandNisthenumberofcoordinatesforeachdatapoint.The
%coordinatesforeachclustercenterarereturnedintherowsofthematrix
%CENTER.ThemembershipfunctionmatrixUcontainsthegradeofmembershipof
%eachDATApointineachcluster.Thevalues0and1indicatenomembership
%andfullmembershiprespectively.Gradesbetween0and1indicatethatthe
%datapointhaspartialmembershipinacluster.Ateachiteration,an
%objectivefunctionisminimizedtofindthebestlocationfortheclusters
%anditsvaluesarereturnedinOBJ_FCN.
%
%[CENTER,...]=FCM(DATA,N_CLUSTER,OPTIONS)specifiesavectorofoptions
%fortheclusteringprocess:
%OPTIONS(1):exponentforthematrixU(default:2.0)
%OPTIONS(2):maximumnumberofiterations(default:100)
%OPTIONS(3):minimumamountofimprovement(default:1e5)
%OPTIONS(4):infodisplayduringiteration(default:1)
%Theclusteringprocessstopswhenthemaximumnumberofiterations
%isreached,orwhentheobjectivefunctionimprovementbetweentwo
%consecutiveiterationsislessthantheminimumamountofimprovement
%specified.UseNaNtoselectthedefaultvalue.
%
%Example
%data=rand(100,2);
%[center,U,obj_fcn]=fcm(data,2);
%plot(data(:,1),data(:,2),'o');
%holdon;
%maxU=max(U);
%%Findthedatapointswithhighestgradeofmembershipincluster1
%index1=find(U(1,:)==maxU);
%%Findthedatapointswithhighestgradeofmembershipincluster2
%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');
%%Plottheclustercenters
%plot([center([12],1)],[center([12],2)],'*','color','k')
%holdoff;
%
%SeealsoFCMDEMO,INITFCM,IRISFCM,DISTFCM,STEPFCM.
%RogerJang,121394,N.Hickey041601
%Copyright19942002TheMathWorks,Inc.
%$Revision:1.13$$Date:2002/04/1422:20:38$
ifnargin~=2&nargin~=3,
error('Toomanyortoofewinputarguments!');
end
data_n=size(data,1);
in_n=size(data,2);
%Changethefollowingtosetdefaultoptions
default_options=[2; %exponentforthepartitionmatrixU
100;
%max.numberofiteration
1e5; %min.amountofimprovement
0];
%infodisplayduringiteration
ifnargin==2,
options=default_options;
else
%If"options"isnotfullyspecified,paditwithdefaultvalues.
iflength(options)<4,
tmp=default_options;
tmp(1:length(options))=options;
options=tmp;
end
%Ifsomeentriesof"options"arenan's,replacethemwithdefaults.
nan_index=find(isnan(options)==1);
options(nan_index)=default_options(nan_index);
ifoptions(1)<=1,
error('Theexponentshouldbegreaterthan1!');
end
end
expo=options(1);
%ExponentforU
https://www.mathworks.com/matlabcentral/fileexchange/48686-fuzzyclustertoolbox/content/FuzzyClusterToolBox/FCM_Matlab/fcm.m
1/2
19/10/2016
max_iter=options(2);
min_impro=options(3);
display=options(4);
%Max.iteration
%Min.improvement
%Displayinfoornot
obj_fcn=zeros(max_iter,1);
%Arrayforobjectivefunction
U=initfcm(cluster_n,data_n);
%Initialfuzzypartition
%Mainloop
fori=1:max_iter,
[U,center,obj_fcn(i)]=stepfcm(data,U,cluster_n,expo);
ifdisplay,
fprintf('Iterationcount=%d,obj.fcn=%f\n',i,obj_fcn(i));
end
%checkterminationcondition
ifi>1,
ifabs(obj_fcn(i)obj_fcn(i1))<min_impro,break;end,
end
end
iter_n=i;
%Actualnumberofiterations
obj_fcn(iter_n+1:max_iter)=[];
https://www.mathworks.com/matlabcentral/fileexchange/48686-fuzzyclustertoolbox/content/FuzzyClusterToolBox/FCM_Matlab/fcm.m
2/2