user1416605
user1416605

Reputation: 71

How to change color of cluster points in matlab

I am implementing AP clustering algorithm. I don't know how to assign different colors to different cluster points.

My code segment is:

    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;

I have to set a different color to different cluster members like red for cluster one, blue for second one and so on.

How can I do this?

Upvotes: 1

Views: 2841

Answers (1)

kitchenette
kitchenette

Reputation: 1623

Here is an example for how to plot one cluster in red dots and one in green plus signs:

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

Now just get your data into the same form as cluster1 and cluster 2 (matrices of the points in cluster 1 and cluster 2) and then you can plot them.

Let's say you don't have a fixed number of clusters. Then you can do this:

%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

Use this link on colorspec from petrichor's comment to learn about all the different combinations of symbols/colors you can define.

Upvotes: 4

Related Questions