gran_profaci
gran_profaci

Reputation: 8461

Color Time Series Based on The Cluster Assignment

Say I have the following Time Series:

a = [1 3 1 5 1 3 5 1 5];

Now, on doing kmeans(a,3), I obtained:

b = kmeans(a,3); // [1 2 1 3 1 2 3 1 3]; based on the clusters.

I now wish to plot a, so that the color for a(i) corresponds to the cluster that is has been assigned in b. Can someone show me how to do that?

Upvotes: 0

Views: 69

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112749

This plots each point with increasing x coordinates, y coordinate equal to a, and the color as given by b:

colors = hsv(max(b)); %// or use other color maps: 
hold on
for ii = 1:length(a)
  plot(ii,a(ii),'marker', 'o', 'color',colors(b(ii),:)) %// option 1
end
axis([0 length(a+1) min(a)-1 max(a)+1])

enter image description here

Upvotes: 1

Related Questions