?? dfknc.m
字號(hào):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 程序運(yùn)行過程中調(diào)用:d函數(shù),這個(gè)函數(shù)計(jì)算在本聚類模式下的兩個(gè)模糊數(shù)的距離
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function DFKNC(c,mm,x,e,a,b)%其中,e表示預(yù)設(shè)的閾值;c類,mm表示模糊關(guān)系數(shù),
%x為待聚類的模糊數(shù)據(jù)矩陣(依次為中心,左區(qū)間長和右區(qū)間長),每一行表示一個(gè)模糊數(shù);a,b分別充當(dāng)lambda和rho的角色
n=size(x);
n=n(1); %求出x的模糊數(shù)的個(gè)數(shù)n
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 判斷類別數(shù)是不是大于數(shù)據(jù)個(gè)數(shù) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if c>n
fprintf('ERROR! The number of clusters is greater than the number of the data, namely %d>%d. Please revise your cluster number!\n',c,n)
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 以下為定義變量部分:
% 第一部分聲明初始關(guān)系矩陣空間
% 第二部分聲明輸入數(shù)據(jù)的中心,左區(qū)間長,右區(qū)間長三個(gè)空間
% 第三部分聲明初始類中心的中心,左區(qū)間長,右區(qū)間長 三個(gè)空間
% 第四部分對(duì)以上的兩個(gè)中心,左區(qū)間長,右區(qū)間長的變量進(jìn)行賦值
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u=ones(c,n)/n; %第一部分
m=ones(1,n); %第二部分
l=ones(1,n);
r=ones(1,n);
M=ones(1,c); %第三部分
L=ones(1,c);
R=ones(1,c);
for i=1:n %第四部分
m(i)=x(i,1);
l(i)=x(i,2);
r(i)=x(i,3);
end
for i=1:c
M(i)=x(i,1)-0.1;
L(i)=x(i,2);
R(i)=x(i,3);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 以下為定義變量部分:
% 第五部分聲明修改后的類中心的中心,左區(qū)間長,右區(qū)間長三個(gè)空間
% 第六部分計(jì)算優(yōu)化后的的隸屬度矩陣uu
% 第七部分計(jì)算修改后的類中心的中心,左區(qū)間長,右區(qū)間長,并進(jìn)行賦值
% 第八部分對(duì)修改后的矩陣進(jìn)行空間聲明,同時(shí)進(jìn)行計(jì)算賦值,其中用到的公式已經(jīng)在論文中有提及
% 第九部分將計(jì)算所得的類中心和左右區(qū)間長賦值給center變量,準(zhǔn)備作為輸出結(jié)果
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
LL=ones(1,c);
RR=ones(1,c);
uu=u;
for j=1:n %第六部分
for i=1:c
dd(i)=1/(d([M(i),L(i),R(i)],[m(j),l(j),r(j)],a,b)^(2/(mm-1)));
end
for i=1:c
uu(i,j)=1/(d([M(i),L(i),R(i)],[m(j),l(j),r(j)],a,b)^(2/(mm-1))*sum(dd));
end
dd=ones(1,c);
end
for i=1:c %第七部分
MM(i)=sum((uu(i,:).^mm).*(3*m-a*(l-L(i))+b*(r-R(i))))/(3*sum(uu(i,:).^mm));
LL(i)=sum((uu(i,:).^mm).*(M(i)+a*l-m))/(3*sum(uu(i,:).^mm));
RR(i)=sum(uu(i,:).^mm.*(m+a*r-M(i)))/(a*sum(uu(i,:).^mm));
end
center(:,1)=MM(:); %第九部分
center(:,2)=LL(:);
center(:,3)=RR(:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 第十部分,進(jìn)入循環(huán),循環(huán)條件為:u矩陣和uu矩陣中相應(yīng)元素的差大于閾值e,則進(jìn)入循環(huán),將新計(jì)算的結(jié)果賦給對(duì)應(yīng)的舊變量,重復(fù)第六部分到第九部分;否則退出循環(huán)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while max(max(abs(u-uu)))>e %第十部分
u=uu;
M(:)=MM(:);
L(:)=LL(:);
R(:)=RR(:);
for i=1:c
MM(i)=sum((uu(i,:).^mm).*(3*m-a*(l-L(i))+b*(r-R(i))))/(3*sum(uu(i,:).^mm));
LL(i)=sum((uu(i,:).^mm).*(M(i)+a*l-m))/(3*sum(uu(i,:).^mm));
RR(i)=sum(uu(i,:).^mm.*(m+a*r-M(i)))/(a*sum(uu(i,:).^mm));
end
dd=ones(1,c);
uu=u;
for j=1:n
for i=1:c
dd(i)=1/d([MM(i),LL(i),RR(i)],[m(j),l(j),r(j)],a,b)^(2/(mm-1));
end
for i=1:c
uu(i,j)=1/(d([MM(i),LL(i),RR(i)],[m(j),l(j),r(j)],a,b)^(2/(mm-1))*sum(dd));
end
end
center(:,1)=MM(:);
center(:,2)=LL(:);
center(:,3)=RR(:);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 輸出結(jié)果:第一個(gè)為類中心,第二個(gè)為隸屬函數(shù)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('The centers of the %d clustering is:\n',c)
center
fprintf('The membership of the elements relating to the %d clusters is:\n',c)
uu=uu'
fprintf('The graph has shown the result of the clustering, in which the red point is the center of the clusters.\n')
fprintf('Attention: Because of the axis in the two graphs is not constrained!.\n')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 作圖,上面一張圖表示原始數(shù)據(jù)(這里的隸屬函數(shù)都認(rèn)為是三角關(guān)系);第二張圖為各類的模糊數(shù)據(jù)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(2,1,1)
hold on
for i=1:n
plot([m(i)-l(i),m(i),m(i)+r(i)],[0,1,0]);
end
hold off
title('The figure of the original data')
subplot(2,1,2)
hold on
for i=1:c
plot([MM(i)-LL(i),MM(i),MM(i)+RR(i)],[0,1,0]);
end
title('The centers and spreads of the cluters')
hold off
end
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -