你好,我是垫子实验室的新手。我正在实现AP聚类算法。我必须识别簇头并用不同的符号表示它。
n=50;
for i=1:n
X(i,1)=rand()*500;
X(i,2)=rand()*500;
end
for i=1:n
plotcircle(X(i,1),X(i,2),3,'r');
end
x=X(:,1);
y=X(:,2);
for i=1:n
for j=i:n
distance(i,j)=(sqrt((X(i,1)-X(j,1))^2 +(X(i,2)-X(j,2))^2 ));
distance(j,i)=distance(i,j);
end
end
Availability=zeros(n,n);
Responsibility=zeros(n,n);
distance=distance+1e-2*randn(n,n)*(max(distance(:))-min(distance(:)));
factor=0.5;
for iter=1:100
PrevResp=Responsibility;
AvailabilityS=Availability+distance;
[X1,I]=max(AvailabilityS,[],2);
for i=1:n
AvailabilityS(i,I(i))=-1000;
end;
[Y2,I2]=max(AvailabilityS,[],2);
Responsibility=distance-repmat(X1,[1,n]);
for i=1:n
Responsibility(i,I(i))=distance(i,I(i))-Y2(i);
end;
Responsibility=(1-factor)*Responsibility+factor*PrevResp;
Aold=Availability;
Rp=max(Responsibility,0);
for k=1:n
Rp(k,k)=Responsibility(k,k);
end;
Availability=repmat(sum(Rp,1),[n,1])-Rp;
dA=diag(Availability);
Availability=min(Availability,0);
for k=1:n
Availability(k,k)=dA(k);
end;
Availability=(1-factor)*Availability+factor*Aold;
end;
E=Responsibility+Availability
I=find(diag(E)>0) % Number of Cluster head
K=length(I);% Number of clusters
fprintf('Number_of_clusters:''%d',length(I))
[tmp c]=max(distance(:,I),[],2);
c(I)=1:K ;
idx=I(c)
for k=1:K
ii=find(c==k)% group data points to k clusters
end;
selected=X(I,:);% ERROR-----------select cluster head but always at boundaries
plot(x(I),y(I),'b*');
第一个错误-----簇头在边界处绘制 第二个问题是我们必须绘制不同颜色的ii簇
请帮助我... plz plz
答案 0 :(得分:0)
如果你把它放在matlab中,你会得到不同颜色的簇:
n=50;
X = 500*rand([n,2]);
% Computing the euclidean distance between each pair of 50 points
distance = zeros(n); %[50 x 50] matrix
for i=1:n
for j=i:n
distance(i,j)=(sqrt((X(i,1)-X(j,1))^2 +(X(i,2)-X(j,2))^2 ));
distance(j,i)=distance(i,j);
end
end
Availability=zeros(n,n);
Responsibility=zeros(n,n);
distance=distance+1e-2*randn(n,n)*(max(distance(:))-min(distance(:)));
factor=0.5;
for iter=1:100
PrevResp=Responsibility;
AvailabilityS=Availability+distance;
[X1,I]=max(AvailabilityS,[],2);
for i=1:n
AvailabilityS(i,I(i))=-1000;
end;
[Y2,I2]=max(AvailabilityS,[],2);
Responsibility=distance-repmat(X1,[1,n]);
for i=1:n
Responsibility(i,I(i))=distance(i,I(i))-Y2(i);
end
Responsibility=(1-factor)*Responsibility+factor*PrevResp;
Aold=Availability;
Rp=max(Responsibility,0);
for k=1:n
Rp(k,k)=Responsibility(k,k);
end;
Availability=repmat(sum(Rp,1),[n,1])-Rp;
dA=diag(Availability);
Availability=min(Availability,0);
for k=1:n
Availability(k,k)=dA(k);
end
Availability=(1-factor)*Availability+factor*Aold;
end
E=Responsibility+Availability
I=find(diag(E)>0) % Number of Cluster head
K=length(I);% Number of clusters
fprintf('Number_of_clusters:''%d',length(I))
C = zeros(n,1);
for j = 1:n,
d = zeros(length(I),1);
for i = 1:length(I),
d(i) = distance(j, I(i))
end
[mval,midx] = min(d)
C(j) = midx;
end
symbs= {'r+','g.','bv','m*'};
hold on
for i = 1: length(I),
plot(X(C==i,1),X(C==i,2),symbs{i})
plot(X(I(i),1),X(I(i),2),symbs{i},'MarkerSize',20)
end