2012-05-25 35 views
1

我正在實現AP聚類算法。我不知道如何爲不同的聚類點分配不同的顏色。如何更改matlab中聚類點的顏色

我的代碼段是:

I=find(diag(E)>0) % Find number of cluster head 
    K=length(I); %cluster head 
    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)% cluster points 
    end; 

我必須設置不同的顏色,以不同的集羣成員,如紅色爲羣集中的一個,藍色爲第二個等等。

我該怎麼做?

+0

你沒有做任何繪圖,所以你想在哪裏設置顏色? – Ansari

+0

你可能會考慮使用['scatter'](http://www.mathworks.com/help/techdoc/ref/scatter.html)[就像這個SO問題](http://stackoverflow.com/q/7692174/677667)。具體而言,您可以通過編程來更改標記類型。 –

回答

4

這裏是如何繪製在紅點一個集羣,一個在綠色加號的例子:

n = 100; 
cluster1 = randn([n,2]); % 100 2-D coordinates 
cluster2 = randn([n,2]); % 100 2-D coordinates 
hold on 
plot(cluster1(:,1),cluster1(:,2),'r.'); %plotting cluster 1 pts 
plot(cluster2(:,1),cluster2(:,2),'g+'); %plotting cluster 2 pts 

enter image description here

現在只是讓你的數據在同一個形式cluster1cluster 2(羣1和羣2中點的矩陣),然後可以繪製它們。

假設您沒有固定數量的羣集。然後,你可以這樣做:

%Defines some order of colors/symbols 
symbs = {'r.', 'g+','m*','b.','ko','y+'}; 

figure(1) 
hold on 
for i = 1:num_clusters, 
    % Some code here to extract the coordinates in one particular cluster... 
    plot(cluster(:,1),cluster(:,2),symbs{i}); 
end 

使用上colorspec此鏈接從的Petrichor的評論來了解所有的符號/顏色可以定義不同的組合。

+0

但集羣數量不固定。 – user1416605

+0

@ user1416605您可以相應地修改您的代碼。在「堅持」之後,您可以繪製任意數量的羣集。要設置顏色programmaticaly,請看這裏:http://www.mathworks.com/help/techdoc/ref/colorspec.html – petrichor

+0

@ user1416605沒問題,該解決方案已修改,向您展示如何處理可變數量的羣集。感謝一羣爲了使這個解決方案更好而使用petrichor。 :) – kitchenette