MATLAB-FCM 源代码修正
2017-01-16 by:CAE仿真在线 来源:互联网
在MATLAB toolbox中有模糊C均值聚类的源代码程序,该程序为经典FCM,其中距离采用欧氏距离计算。
在实际应用中,可能会遇到这样的情况,(1)想更换距离计算公式,采用如马氏距离,或者其它距离(详细可参见MATLAB 统计工具箱:pdist,pdist2,mahal,etc.)
以下帮你修改源程序,来实现以上目的。
1) 打开fcm,将options类据类型按元胞设置,这样可以存放字符串数据。修改部分见下面。最后另存为fcm1
% Change the following to set default options
default_options = {2; % exponent for the
partition matrix U
100; % max.
number of iteration
1e-5; % min.
amount of improvement
1; % info
display during iteration
'euclidean'}; % distance
if nargin == 2,
options = default_options;
else
% If "options" is not fully specified, pad it
with default values.
if length(options)
< 5,
for i=length(options)+1:5
options{i}=default_options{i};
end
end
% If some entries of "options" are nan's, replace
them with defaults.
nan_index = false(5,1);
for i
=1:length(options)
if isnan(options{i})
nan_index(i) = true;
else
nan_index(i) = false;
end
end
options(nan_index) =
default_options(nan_index);
if options{1} <= 1,
error('The exponent should be
greater than 1!');
end
end
[U, center, obj_fcn(i)] = stepfcm1(data, U, cluster_n, expo, options{5:end});
2)打开stepfcm,将dist计算公式更换为以下形式。文件另存为stdpfcm1
function [U_new, center, obj_fcn] = stepfcm1(data, U, cluster_n, expo, distance,varargin)
dist = pdist2(data, center, distance, varargin{:})'; % fill the distance matrix
----------------------------------------
以上修改完成后,调用格式也发生微小变化。
[center,U,obj_fcn] = fcm(data,cluster_n)[center,U,obj_fcn] = fcm(data,cluster_n,options)原始 options内容: options =[2,100,1e-6,1]改后options内容:options={2,100,1e-6,1} 默认为欧氏距离计算,改后的options还可以再增加options ={2,100,1e-6,1,'mahalanobis'};options ={2,100,1e-6,1,'mahalanobis',C};options ={2,100,1e-6,1,'minkowski',p};所增加的内容数据格式与pdist2相同。见以下红色字体D = pdist2(X,Y) D = pdist2(X,Y,distance) D = pdist2(X,Y,'minkowski',P) D = pdist2(X,Y,'mahalanobis',C)
相关标签搜索:MATLAB-FCM 源代码修正 MatLab培训 MatLab培训课程 MatLab在线视频教程 MatLab技术学习教程 MatLab软件教程 MatLab资料下载 MatLab代做 MatLab基础知识 Fluent、CFX流体分析 HFSS电磁分析 Ansys培训 Abaqus培训