在MATLAB7安装文件夹\toolbox\fuzzy\fuzzy下有几个m文件,其中fcm.m就是源码。

解决方案 »

  1.   


    FCM的源代码:
    function [center, U, obj_fcn] = fcm(data, cluster_n, options)
    %FCM Data set clustering using fuzzy c-means clustering.
    %
    %   [CENTER, U, OBJ_FCN] = FCM(DATA, N_CLUSTER) finds N_CLUSTER number of
    %   clusters in the data set DATA. DATA is size M-by-N, where M is the number of
    %   data points and N is the number of coordinates for each data point. The
    %   coordinates for each cluster center are returned in the rows of the matrix
    %   CENTER. The membership function matrix U contains the grade of membership of
    %   each DATA point in each cluster. The values 0 and 1 indicate no membership
    %   and full membership respectively. Grades between 0 and 1 indicate that the
    %   data point has partial membership in a cluster. At each iteration, an
    %   objective function is minimized to find the best location for the clusters
    %   and its values are returned in OBJ_FCN.
    %
    %   [CENTER, ...] = FCM(DATA,N_CLUSTER,OPTIONS) specifies a vector of options
    %   for the clustering process:
    %       OPTIONS(1): exponent for the matrix U             (default: 2.0)
    %       OPTIONS(2): maximum number of iterations          (default: 100)
    %       OPTIONS(3): minimum amount of improvement         (default: 1e-5)
    %       OPTIONS(4): info display during iteration         (default: 1)
    %   The clustering process stops when the maximum number of iterations
    %   is reached, or when the objective function improvement between two
    %   consecutive iterations is less than the minimum amount of improvement
    %   specified. Use NaN to select the default value.
    %
    %   Example
    %       data = rand(100,2);
    %       [center,U,obj_fcn] = fcm(data,2);
    %       plot(data(:,1), data(:,2),'o');
    %       hold on;
    %       maxU = max(U);
    %       % Find the data points with highest grade of membership in cluster 1
    %       index1 = find(U(1,:) == maxU);
    %       % Find the data points with highest grade of membership in cluster 2
    %       index2 = find(U(2,:) == maxU);
    %       line(data(index1,1),data(index1,2),'er','*','color','g');
    %       line(data(index2,1),data(index2,2),'er','*','color','r');
    %       % Plot the cluster centers
    %       plot([center([1 2],1)],[center([1 2],2)],'*','color','k')
    %       hold off;
    %
    %   See also FCMDEMO, INITFCM, IRISFCM, DISTFCM, STEPFCM.%   Roger Jang, 12-13-94, N. Hickey 04-16-01
    %   Copyright 1994-2002 The MathWorks, Inc. 
    %   $Revision: 1.13 $  $Date: 2002/04/14 22:20:38 $if nargin ~= 2 & nargin ~= 3,
    error('Too many or too few input arguments!');
    enddata_n = size(data, 1);
    in_n = size(data, 2);% 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 if nargin == 2,
    options = default_options;
    else
    % If "options" is not fully specified, pad it with default values.
    if length(options) < 4,
    tmp = default_options;
    tmp(1:length(options)) = options;
    options = tmp;
    end
    % If some entries of "options" are nan's, replace them with defaults.
    nan_index = find(isnan(options)==1);
    options(nan_index) = default_options(nan_index);
    if options(1) <= 1,
    error('The exponent should be greater than 1!');
    end
    endexpo = options(1); % Exponent for U
    max_iter = options(2); % Max. iteration
    min_impro = options(3); % Min. improvement
    display = options(4); % Display info or notobj_fcn = zeros(max_iter, 1); % Array for objective functionU = initfcm(cluster_n, data_n); % Initial fuzzy partition
    % Main loop
    for i = 1:max_iter,
    [U, center, obj_fcn(i)] = stepfcm(data, U, cluster_n, expo);
    if display, 
    fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i));
    end
    % check termination condition
    if i > 1,
    if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end,
    end
    enditer_n = i; % Actual number of iterations 
    obj_fcn(iter_n+1:max_iter) = [];
      

  2.   

    你先找一些理论知识看下。这里给出算法步骤,直接给你fast fcm,做论文,跑程序都方便。参考文献:一种用于磁共振颅脑图像分割的快速模糊聚类算法 《中国生物医学工程学报》1 确定参数c,n,m,e普西龙d,b=0
    c是聚类数,m是模糊加权指数,e普西龙是迭代终止参数,或者叫收敛门限,n是总类数。这几个参数是没什么问题的,其他的解释下
    n-像素总数,用来进行k均值的,目的是加快分割速度。
    d-向量的特征数,这个应该知道的
    b是迭代用的参数其中,m=1,fcm就变成了hcm。很多文献上都说到这个2 用k-均值聚类进行初始分割,把分割结果作为fcm的初始聚类中心V=(v1,v2,....,vc)
    这步的好处是,让后面进行迭代的次数少了很多。毕竟fcm的复杂度是很大的。
    k-均值步骤我后面说这里后面和普通的fcm一样
    3 更新U(b)到U(b+1)。 任取i,j
    根据fcm定理。这个很多文献上都有的吧
    uik=...一堆,这里不打了。关键字搜索,fcm定理。其中有一点if (dij!=0,j=1,2,....c)   //这里dij是数据点xk与第i类中心vi之间的距离,记得距离要取范数
       根据fcm定理,更新uik
    if (dij=0)
      uik=1
    if (dtj=0 , t!=i)
      uik=0要加两个边界条件4 根据U(b+1)和fcm定理,计算c各均值矢量vi(b)
    这步应该也没问题的,代公式就好5 比较U(b)和U(b+1),若||U(b)-U(b+1)||<e普西龙,停止迭代,否则b++,返回3。上面是ffcm的步骤。下面给出k-means步骤,用伪代码给出1 把图像随意分成nc个区域,并且进行标记,用1,2,3....nc
    2 计算每一类的均值向量  mc[i][c],i=1.....d,c=1....nc
      d  向量的特征数
    3 进行下述为代码流程
    change=false
    for j=1...N  do
    Distmin=Bignumber,class=0
    //寻找最近均值
       for c=1...nc do        // nc 聚类数
       Dist=Distance(xj,mc)   //  xij  待聚类的像素向量,mc 每一类的均值向量
                                  // 这步用来计算xj到均值mc的距离
       if Dist<Distmin then Distmin=Dist
         class=c
       labj=class; 
       如果像素标记改变
          change=True;
    if change=ture
      返回2,重新计算mc,并且进行上述操作 
      

  3.   

    http://blog.csdn.net/laviewpbt/archive/2006/11/01/1361548.aspx模糊聚类算法(FCM)和硬聚类算法(HCM)的VB6.0实现及其应用