MATLAB-FCM 源代碼修正
2017-01-16 by:CAE仿真在線 來源:互聯(lián)網(wǎng)
在MATLAB toolbox中有模糊C均值聚類的源代碼程序,該程序為經(jīng)典FCM,其中距離采用歐氏距離計算。
在實際應用中,可能會遇到這樣的情況,(1)想更換距離計算公式,采用如馬氏距離,或者其它距離(詳細可參見MATLAB 統(tǒng)計工具箱:pdist,pdist2,mahal,etc.)
以下幫你修改源程序,來實現(xiàn)以上目的。
1) 打開fcm,將options類據(jù)類型按元胞設置,這樣可以存放字符串數(shù)據(jù)。修改部分見下面。最后另存為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
----------------------------------------
以上修改完成后,調用格式也發(fā)生微小變化。
[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};所增加的內容數(shù)據(jù)格式與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培訓