Sigma = [1, 0; 0, 1];
mu1 = [1, -1];
x1 = mvnrnd(mu1, Sigma, 200);
mu2 = [5.5, -4.5];
x2 = mvnrnd(mu2, Sigma, 200);
mu3 = [1, 4];
x3 = mvnrnd(mu3, Sigma, 200);
mu4 = [6, 4.5];
x4 = mvnrnd(mu4, Sigma, 200);
mu5 = [9, 0.0];
x5 = mvnrnd(mu5, Sigma, 200);
% obtain the 1000 data points to be clustered
X = [x1; x2; x3; x4; x5];
% Show the data point
plot(x1(:,1), x1(:,2), 'r.'); hold on;
plot(x2(:,1), x2(:,2), 'b.');
plot(x3(:,1), x3(:,2), 'k.');
plot(x4(:,1), x4(:,2), 'g.');
plot(x5(:,1), x5(:,2), 'm.');
save myX %将X存储到文件中,在其他文件中load就可以了
结果如下:
% 初始聚类中心
mu0=[X(,:); X(,:);X(,:);X(,:);X(,:)];
mu1=zeros(,c);
lable=zeros(r,);
% first cluster
dist=zeros(,);
for k=:r
for n=:
dist(n)=norm(X(k,:)-mu0(n,:));
end
lable(k)=find(dist==min(dist));
end % X1=[lable X];
sum=zeros(,);
count=zeros(,); % 第一次聚类
for n=:
for k=:r
if lable(k)==n
sum(n,:)=sum(n,:)+X(k,:);
count(n)=count(n)+;
end
end
mu1(n,:)=sum(n,:)/count(n);
end % square error
e=zeros(,);
esum=;
for n=:
for k=:r
if lable(k)==n
e(n,:)=e(n,:)+norm(X(k,:)-mu1(n,:));
end
end
esum=esum+e(n,:);
end % ***************** start Iteration ************** countNotX=;% 到达一定值后,停止迭代
s=;
count1=zeros(,);
while true
s=s+;
%random choose X to update
ran=round(+*rand());%总共1000个样本
Xrand=X(ran,:);
rou=zeros(,);
rouMin=inf;
newClassLable=;
if count(lable(ran))~=
for n=:
if lable(ran)==n
rou(n)=norm(Xrand-mu1(n,:))*count(n)/(count(n)-);
else
rou(n)=norm(Xrand-mu1(n,:))*count(n)/(count(n)+);
end
if rou(n)<=rouMin;
rouMin=rou(n);
newClassLable=n;
end
end
if rouMin<rou(lable(ran))
countNotX=;
% new mu 这里用课件中的公式,不知道哪里错了,总是不行,就用最蠢的办法了
for n=:
for k=:r
if lable(k)==n
sum(n,:)=sum(n,:)+X(k,:);
count1(n)=count1(n)+;
end
end
mu1(n,:)=sum(n,:)/count1(n);
end
% new count
count(lable(ran))=count(lable(ran))-;
count(newClassLable)=count(newClassLable)+;
% new esum
e(lable(ran),:)=e(lable(ran),:)-rou(lable(ran));
e(newClassLable,:)=e(newClassLable,:)+rouMin;
esum=esum+rouMin-rou(lable(ran));
lable(ran)=newClassLable ;
else
% disp('No')
countNotX= countNotX+;
if countNotX==
%when J is not changed for times,reckon OK
break
end
end
end
end