📄 dfknc.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 程序运行过程中调用:d函数,这个函数计算在本聚类模式下的两个模糊数的距离
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function DFKNC(c,mm,x,e,a,b)%其中,e表示预设的阈值;c类,mm表示模糊关系数,
%x为待聚类的模糊数据矩阵(依次为中心,左区间长和右区间长),每一行表示一个模糊数;a,b分别充当lambda和rho的角色
n=size(x);
n=n(1); %求出x的模糊数的个数n
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 判断类别数是不是大于数据个数 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 以下为定义变量部分:
% 第一部分声明初始关系矩阵空间
% 第二部分声明输入数据的中心,左区间长,右区间长三个空间
% 第三部分声明初始类中心的中心,左区间长,右区间长 三个空间
% 第四部分对以上的两个中心,左区间长,右区间长的变量进行赋值
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 以下为定义变量部分:
% 第五部分声明修改后的类中心的中心,左区间长,右区间长三个空间
% 第六部分计算优化后的的隶属度矩阵uu
% 第七部分计算修改后的类中心的中心,左区间长,右区间长,并进行赋值
% 第八部分对修改后的矩阵进行空间声明,同时进行计算赋值,其中用到的公式已经在论文中有提及
% 第九部分将计算所得的类中心和左右区间长赋值给center变量,准备作为输出结果
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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(:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 第十部分,进入循环,循环条件为:u矩阵和uu矩阵中相应元素的差大于阈值e,则进入循环,将新计算的结果赋给对应的旧变量,重复第六部分到第九部分;否则退出循环
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 输出结果:第一个为类中心,第二个为隶属函数
%%%%%%%%%%%%%%%%%%%%%%%%%%%
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')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 作图,上面一张图表示原始数据(这里的隶属函数都认为是三角关系);第二张图为各类的模糊数据
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -