FCM,PCM 聚类算法 MATLAB 程序
function [U,center,result,w,obj_fcn]= fenlei(data)
[data_n,in_n] = size(data);
m= 2; % Exponent for U
max_iter = 100; % Max. iteration
min_impro =1e-5; % Min. improvement
c=3;
[center, U, obj_fcn] = fcm(data, c);
for i=1:max_iter
if F(U)>0.98
break;
else
w_new=eye(in_n,in_n);
center1=sum(center)/c;
a=center1(1)./center1;
deta=center-center1(ones(c,1),:);
w=sqrt(sum(deta.^2)).*a;
for j=1:in_n
w_new(j,j)=w(j);
end
data1=data*w_new;
[center, U, obj_fcn] = fcm(data1, c);
center=center./w(ones(c,1),:);
obj_fcn=obj_fcn/sum(w.^2);
end
end
display(i);
result=zeros(1,data_n);U_=max(U);
for i=1:data_n
for j=1:c
if U(j,i)==U_(i)
result(i)=j;continue;
end
end
end
聚类算法 Kmeans-----matlab code
function [cid,nr,centers] = cskmeans(x,k,nc)
% CSKMEANS K-Means clustering - general method.
%
% This implements the more general k-means algorithm, where
% HMEANS is used to find the initial partition and then each
% observation is examined for further improvements in minimizing
% the within-group sum of squares.
%
% [CID,NR,CENTERS] = CSKMEANS(X,K,NC) Performs K-means
% clustering using the data given in X.
%
% INPUTS: X is the n x d matrix of data,
% where each row indicates an observation. K indicates
% the number of desired clusters. NC is a k x d matrix for the
% initial cluster centers. If NC is not specified, then the
% centers will be randomly chosen from the observations.
%
% OUTPUTS: CID provides a set of n indexes indicating cluster
% membership for each point. NR is the number of observations
% in each cluster. CENTERS is a matrix, where each row
% corresponds to a cluster center.
%
% See also CSHMEANS
% W. L. and A. R. Martinez, 9/15/01
% Computational Statistics Toolbox
warning off
[n,d] = size(x);
if nargin < 3
% Then pick some observations to be the cluster centers.
ind = ceil(n*rand(1,k));
% We will add some noise to make it interesting.
nc = x(ind,:) + randn(k,d);
end
% set up storage
% integer 1,...,k indicating cluster membership
cid = zeros(1,n);
% Make this different to get the loop started.
oldcid = ones(1,n);
% The number in each cluster.
nr = zeros(1,k);
% Set up maximum number of iterations.
maxiter = 100;
iter = 1;
while ~isequal(cid,oldcid) & iter < maxiter
% Implement the hmeans algorithm
% For each point, find the distance to all cluster centers
for i = 1:n
dist = sum((repmat(x(i,:),k,1)-nc).^2,2);
[m,ind] = min(dist); % assign it to this cluster center
cid(i) = ind;
end
% Find the new cluster centers
for i = 1:k
% find all points in this cluster
ind = find(cid==i);
% find the centroid
nc(i,:) = mean(x(ind,:));
% Find the number in each cluster;
nr(i) = length(ind);
end
iter = iter + 1;
end
% Now check each observation to see if the error can be minimized some more.
% Loop through all points.
maxiter = 2;
iter = 1;
move = 1;
while iter < maxiter & move ~= 0
move = 0;
% Loop through all points.
for i = 1:n
% find the distance to all cluster centers
dist = sum((repmat(x(i,:),k,1)-nc).^2,2);
r = cid(i); % This is the cluster id for x
%%nr,nr+1;
dadj = nr./(nr+1).*dist'; % All adjusted distances
[m,ind] = min(dadj); % minimum should be the cluster it belongs to
if ind ~= r % if not, then move x
cid(i) = ind;
ic = find(cid == ind);
nc(ind,:) = mean(x(ic,:));
move = 1;
end
end
iter = iter+1;
end
centers = nc;
if move == 0
disp('No points were moved after the initial clustering procedure.')
else
disp('Some points were moved after the initial clustering procedure.')
end
warning on
function d=data()
%K-means 算法主程序
k=4;
x =[ 1.2126&<60;&<60; 2.1338&<60;&<60; 0.5115&<60;&<60; 0.2044
&<60; -0.9316&<60;&<60; 0.7634&<60;&<60; 0.0125&<60;&<60; -0.2752
&<60; -2.9593&<60;&<60; 0.1813&<60;&<60; -0.8833&<60;&<60; 0.8505
&<60; 3.1104&<60;&<60; -2.5393&<60;&<60; -0.0588&<60;&<60; 0.1808
&<60; -3.1141&<60;&<60; -0.1244&<60;&<60; -0.6811&<60;&<60; 0.9891
&<60; -3.2008&<60;&<60; 0.0024&<60;&<60; -1.2901&<60;&<60; 0.9748
&<60; -1.0777&<60;&<60; 1.1438&<60;&<60; 0.1996&<60;&<60; 0.0139
&<60; -2.7213&<60;&<60; -0.1909&<60;&<60; 0.1184&<60;&<60; 0.1013
&<60; -1.1467&<60;&<60; 1.3820&<60;&<60; 0.1427&<60;&<60; -0.2239
&<60; 1.1497&<60;&<60; 1.9414&<60;&<60; -0.3035&<60;&<60; 0.3464
&<60; 2.6993&<60;&<60; -2.2556&<60;&<60; 0.1637&<60;&<60; -0.0139
&<60; -3.0311&<60;&<60; 0.1417&<60;&<60; 0.0888&<60;&<60; 0.1791
&<60; -2.8403&<60;&<60; -0.1809&<60;&<60; -0.0965&<60;&<60; 0.0817
&<60; 1.0118&<60;&<60; 2.0372&<60;&<60; 0.1638&<60;&<60; -0.0349
&<60; -0.8968&<60;&<60; 1.0260&<60;&<60; -0.1013&<60;&<60; 0.2369
&<60; 1.1112&<60;&<60; 1.8802&<60;&<60; -0.0291&<60;&<60; -0.1506
&<60; 1.1907&<60;&<60; 2.2041&<60;&<60; -0.1060&<60;&<60; 0.2167
&<60; -1.0114&<60;&<60; 0.8029&<60;&<60; -0.1317&<60;&<60; 0.0153
&<60; -3.1715&<60;&<60; 0.1041&<60;&<60; -0.3338&<60;&<60; 0.0321
&<60; 0.9718&<60;&<60; 1.9634&<60;&<60; 0.0305&<60;&<60; -0.3259
&<60; -1.0377&<60;&<60; 0.8889&<60;&<60; -0.2834&<60;&<60; 0.2301
&<60; -0.8989&<60;&<60; 1.0185&<60;&<60; -0.0289&<60;&<60; 0.0213
&<60; -2.9815&<60;&<60; -0.4798&<60;&<60; 0.2245&<60;&<60; 0.3085
&<60; -0.8576&<60;&<60; 0.9231&<60;&<60; -0.2752&<60;&<60; -0.0091
&<60; -3.1356&<60;&<60; 0.0026&<60;&<60; -1.2138&<60;&<60; 0.7733
&<60; 3.4470&<60;&<60; -2.2418&<60;&<60; 0.2014&<60;&<60; -0.1556
&<60; 2.9143&<60;&<60; -1.7951&<60;&<60; 0.1992&<60;&<60; -0.2146
&<60; 3.4961&<60;&<60; -2.4969&<60;&<60; -0.0121&<60;&<60; 0.1315
&<60; -2.9341&<60;&<60; -0.1071&<60;&<60; -0.7712&<60;&<60; 0.8911
&<60;
-0.0287&<60;&<60;
-0.1279
&<60; 3.1006&<60;&<60; -2.0677&<60;&<60; -0.2002&<60;&<60; -0.1303
&<60; 0.8209&<60;&<60; 2.1724&<60;&<60; 0.1548&<60;&<60; 0.3516
&<60; -2.8500&<60;&<60; 0.3196&<60;&<60; 0.1359&<60;&<60; -0.1179
&<60; -2.8679&<60;&<60; 0.1365&<60;&<60; -0.5702&<60;&<60; 0.7626
&<60; -2.8245&<60;&<60; -0.1312&<60;&<60; 0.0881&<60;&<60; -0.1305
&<60; -0.8322&<60;&<60; 1.3014&<60;&<60; -0.3837&<60;&<60; 0.2400
&<60; -2.6063&<60;&<60; 0.1431&<60;&<60; 0.1880&<60;&<60; 0.0487
&<60;
-0.0359&<60;&<60;
-0.2080
&<60; 0.6893&<60;&<60; 2.0854&<60;&<60; -0.3250&<60;&<60; -0.1007
-0.0884&<60;&<60;
-2.8105&<60;&<60;
-3.1341&<60;&<60;
-0.0854&<60;&<60;
-2.9219&<60;&<60;
&<60; 1.0894&<60;&<60; 1.7271&<60;&<60; -0.0176&<60;&<60; 0.6553
&<60; -2.9851&<60;&<60; -0.0113&<60;&<60; 0.0666&<60;&<60; -0.0802
&<60; 1.0371&<60;&<60; 2.2724&<60;&<60; 0.1044&<60;&<60; 0.3982
&<60; -2.8032&<60;&<60; -0.2737&<60;&<60; -0.7391&<60;&<60; 1.0277
&<60; -2.6856&<60;&<60; 0.0619&<60;&<60; -1.1066&<60;&<60; 1.0485
&<60; -2.9445&<60;&<60; -0.1602&<60;&<60; -0.0019&<60;&<60; 0.0093
&<60; 1.2004&<60;&<60; 2.1302&<60;&<60; -0.1650&<60;&<60; 0.3413
&<60; 3.2505&<60;&<60; -1.9279&<60;&<60; 0.4462&<60;&<60; -0.2405
&<60; -1.2080&<60;&<60; 0.8222&<60;&<60; 0.1671&<60;&<60; 0.1576
&<60; -2.8274&<60;&<60; 0.1515&<60;&<60; -0.9636&<60;&<60; 1.0675
&<60; 2.8190&<60;&<60; -1.8626&<60;&<60; 0.2702&<60;&<60; 0.0026
&<60; 1.0507&<60;&<60; 1.7776&<60;&<60; -0.1421&<60;&<60; 0.0999
&<60; -2.8946&<60;&<60; 0.1446&<60;&<60; -0.1645&<60;&<60; 0.3071
&<60; -1.0105&<60;&<60; 1.0973&<60;&<60; 0.0241&<60;&<60; 0.1628
&<60; -2.9138&<60;&<60; -0.3404&<60;&<60; 0.0627&<60;&<60; 0.1286
&<60; -3.0646&<60;&<60; -0.0008&<60;&<60; 0.3819&<60;&<60; -0.1541
&<60; 1.2531&<60;&<60; 1.9830&<60;&<60; -0.0774&<60;&<60; 0.2413
&<60; 1.1486&<60;&<60; 2.0440&<60;&<60; -0.0582&<60;&<60; -0.0650
&<60; -3.1401&<60;&<60; -0.1447&<60;&<60; -0.6580&<60;&<60; 0.9562
&<60; -2.9591&<60;&<60; 0.1598&<60;&<60; -0.6581&<60;&<60; 1.1937
&<60;
-0.1538&<60;&<60;
-0.2085
&<60; 2.8948&<60;&<60; -2.2745&<60;&<60; 0.2332&<60;&<60; -0.0312
&<60;
-0.0288&<60;&<60;
-0.1436
&<60; -1.2737&<60;&<60; 0.7648&<60;&<60; 0.0643&<60;&<60; 0.0858
&<60; -1.0690&<60;&<60; 0.8108&<60;&<60; -0.2723&<60;&<60; 0.3231
&<60; -0.5908&<60;&<60; 0.7508&<60;&<60; -0.5456&<60;&<60; 0.0190
&<60; 0.5808&<60;&<60; 2.0573&<60;&<60; -0.1658&<60;&<60; 0.1709
&<60; 2.8227&<60;&<60; -2.2461&<60;&<60; 0.2255&<60;&<60; -0.3684
&<60; 0.6174&<60;&<60; 1.7654&<60;&<60; -0.3999&<60;&<60; 0.4125
&<60; 3.2587&<60;&<60; -1.9310&<60;&<60; 0.2021&<60;&<60; 0.0800
&<60; 1.0999&<60;&<60; 1.8852&<60;&<60; -0.0475&<60;&<60; -0.0585
&<60; -2.7395&<60;&<60; 0.2585&<60;&<60; -0.8441&<60;&<60; 0.9987
&<60; -1.2223&<60;&<60; 1.0542&<60;&<60; -0.2480&<60;&<60; -0.2795
&<60; -2.9212&<60;&<60; -0.0605&<60;&<60; -0.0259&<60;&<60; 0.2591
&<60; 3.1598&<60;&<60; -2.2631&<60;&<60; 0.1746&<60;&<60; 0.1485
&<60; 0.8476&<60;&<60; 1.8760&<60;&<60; -0.2894&<60;&<60; -0.0354
&<60; 2.9205&<60;&<60; -2.2418&<60;&<60; 0.4137&<60;&<60; -0.2499
&<60; 2.7656&<60;&<60; -2.1768&<60;&<60; 0.0719&<60;&<60; -0.1848
&<60; -0.8698&<60;&<60; 1.0249&<60;&<60; -0.2084&<60;&<60; -0.0008
&<60; -1.1444&<60;&<60; 0.7787&<60;&<60; -0.4958&<60;&<60; 0.3676
&<60; -1.0711&<60;&<60; 1.0450&<60;&<60; -0.0477&<60;&<60; -0.4030
&<60; 0.5350&<60;&<60; 1.8110&<60;&<60; -0.0377&<60;&<60; 0.1622
-0.3637&<60;&<60;
-3.2972&<60;&<60;
-0.0219&<60;&<60;
-2.9322&<60;&<60;
-0.0631&<60;&<60;
&<60; 0.9076&<60;&<60; 1.8845&<60;&<60; -0.1121&<60;&<60; 0.5700
&<60; -2.7887&<60;&<60; -0.2119&<60;&<60; 0.0566&<60;&<60; 0.0120
&<60; -1.2567&<60;&<60; 0.9274&<60;&<60; 0.1104&<60;&<60; 0.1581
&<60; -2.9946&<60;&<60; -0.2086&<60;&<60; -0.8169&<60;&<60; 0.6662
&<60; 1.0536&<60;&<60; 1.9818&<60;&<60; -0.0631&<60;&<60; 0.2581
&<60; -2.8465&<60;&<60; -0.2222&<60;&<60; 0.2745&<60;&<60; 0.1997
&<60; -2.8516&<60;&<60; 0.1649&<60;&<60; -0.7566&<60;&<60; 0.8616
&<60; -3.2470&<60;&<60; 0.0770&<60;&<60; 0.1173&<60;&<60; -0.1092
&<60;
-0.0062&<60;&<60;
-0.0511
&<60; -2.7919&<60;&<60; 0.0438&<60;&<60; -0.1935&<60;&<60; -0.5023
&<60; 0.9894&<60;&<60; 1.9475&<60;&<60; -0.0146&<60;&<60; -0.0390
&<60; -2.9659&<60;&<60; -0.1300&<60;&<60; 0.1144&<60;&<60; 0.3410
&<60; -2.7322&<60;&<60; -0.0427&<60;&<60; -1.0758&<60;&<60; 0.9718
&<60; -1.4852&<60;&<60; 0.8592&<60;&<60; -0.0503&<60;&<60; -0.1373
&<60; 2.8845&<60;&<60; -2.1465&<60;&<60; -0.0533&<60;&<60; -0.1044
&<60; -3.1470&<60;&<60; 0.0536&<60;&<60; 0.1073&<60;&<60; 0.3323
&<60; 2.9423&<60;&<60; -2.1572&<60;&<60; 0.0505&<60;&<60; 0.1180
&<60; -3.0683&<60;&<60; 0.3434&<60;&<60; -0.6563&<60;&<60; 0.8960
&<60; 1.3215&<60;&<60; 2.0951&<60;&<60; -0.1557&<60;&<60; 0.3994
&<60; -0.7681&<60;&<60; 1.2075&<60;&<60; -0.2781&<60;&<60; 0.2372
&<60; -0.6964&<60;&<60; 1.2360&<60;&<60; -0.3342&<60;&<60; 0.1662
&<60; -0.6382&<60;&<60; 0.8204&<60;&<60; -0.2587&<60;&<60; 0.3344
&<60;
-0.2607&<60;&<60;
-0.0400
&<60; -0.8952&<60;&<60; 0.9872&<60;&<60; 0.0019&<60;&<60; 0.3138
&<60; -0.8172&<60;&<60; 0.6814&<60;&<60; -0.0691&<60;&<60; 0.1009
&<60; -3.3032&<60;&<60; 0.0571&<60;&<60; -0.0243&<60;&<60; -0.1405
&<60; 0.7810&<60;&<60; 1.9013&<60;&<60; -0.3996&<60;&<60; 0.7374
&<60; -0.9030&<60;&<60; 0.8646&<60;&<60; -0.1498&<60;&<60; 0.1112
&<60; -0.8461&<60;&<60; 0.9261&<60;&<60; -0.1295&<60;&<60; -0.0727
&<60; 2.8182&<60;&<60; -2.0818&<60;&<60; -0.1430&<60;&<60; -0.0547
&<60; 2.9295&<60;&<60; -2.3846&<60;&<60; -0.0244&<60;&<60; -0.1400
&<60; 1.0587&<60;&<60; 2.2227&<60;&<60; -0.1250&<60;&<60; 0.0957
&<60; 3.0755&<60;&<60; -1.7365&<60;&<60; -0.0511&<60;&<60; 0.1500
&<60; -1.3076&<60;&<60; 0.8791&<60;&<60; -0.3720&<60;&<60; 0.0331
&<60; -2.8252&<60;&<60; -0.0366&<60;&<60; -0.6790&<60;&<60; 0.7374
&<60; -2.6551&<60;&<60; -0.1875&<60;&<60; 0.3222&<60;&<60; 0.0483
&<60; -2.9659&<60;&<60; -0.1585&<60;&<60; 0.4013&<60;&<60; -0.1402
&<60; -3.2859&<60;&<60; -0.1546&<60;&<60; 0.0104&<60;&<60; -0.1781
&<60; -0.6679&<60;&<60; 1.1999&<60;&<60; 0.1396&<60;&<60; -0.3195
&<60; -1.0205&<60;&<60; 1.2226&<60;&<60; 0.1850&<60;&<60; 0.0050
&<60; -3.0091&<60;&<60; -0.0186&<60;&<60; -0.9111&<60;&<60; 0.9663
&<60; -3.0339&<60;&<60; 0.1377&<60;&<60; -0.9662&<60;&<60; 1.0664
-0.1496&<60;&<60;
-3.0233&<60;&<60;