-2

重複の可能性:
matlab でクラスター ポイントの色を変更する方法

こんにちは、マットラボの初心者です。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*');

最初のエラー-----クラスターヘッドは境界にプロットされます 2番目の問題は、iiクラスターを異なる色でプロットする必要があることです

助けてください... お願いします

4

1 に答える 1

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
于 2012-05-25T16:15:27.753 に答える