MATLAB-FCM 源代碼修正
2017-01-16 by:CAE仿真在線 來(lái)源:互聯(lián)網(wǎng)
在MATLAB toolbox中有模糊C均值聚類的源代碼程序,該程序?yàn)榻?jīng)典FCM,其中距離采用歐氏距離計(jì)算。
在實(shí)際應(yīng)用中,可能會(huì)遇到這樣的情況,(1)想更換距離計(jì)算公式,采用如馬氏距離,或者其它距離(詳細(xì)可參見(jiàn)MATLAB 統(tǒng)計(jì)工具箱:pdist,pdist2,mahal,etc.)
以下幫你修改源程序,來(lái)實(shí)現(xiàn)以上目的。
1) 打開(kāi)fcm,將options類據(jù)類型按元胞設(shè)置,這樣可以存放字符串?dāng)?shù)據(jù)。修改部分見(jiàn)下面。最后另存為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)打開(kāi)stepfcm,將dist計(jì)算公式更換為以下形式。文件另存為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
----------------------------------------
以上修改完成后,調(diào)用格式也發(fā)生微小變化。
[center,U,obj_fcn] = fcm(data,cluster_n)[center,U,obj_fcn] = fcm(data,cluster_n,options)原始 options內(nèi)容: options =[2,100,1e-6,1]改后options內(nèi)容:options={2,100,1e-6,1} 默認(rèn)為歐氏距離計(jì)算,改后的options還可以再增加options ={2,100,1e-6,1,'mahalanobis'};options ={2,100,1e-6,1,'mahalanobis',C};options ={2,100,1e-6,1,'minkowski',p};所增加的內(nèi)容數(shù)據(jù)格式與pdist2相同。見(jiàn)以下紅色字體D = pdist2(X,Y) D = pdist2(X,Y,distance) D = pdist2(X,Y,'minkowski',P) D = pdist2(X,Y,'mahalanobis',C)
相關(guān)標(biāo)簽搜索:MATLAB-FCM 源代碼修正 MatLab培訓(xùn) MatLab培訓(xùn)課程 MatLab在線視頻教程 MatLab技術(shù)學(xué)習(xí)教程 MatLab軟件教程 MatLab資料下載 MatLab代做 MatLab基礎(chǔ)知識(shí) Fluent、CFX流體分析 HFSS電磁分析 Ansys培訓(xùn) Abaqus培訓(xùn)