You cannot specify the coloring in DENDROGRAM to match the clusters returned by CLUSTER in the Statistics Toolbox. To work around this limitation, you can use the "colorthreshold" option in the DENDROGRAM function as follows:
NumCluster = 3;
rand('state', 7)
data = [rand(10,3); rand(10,3)+1; rand(10,3)+2];
dist = pdist(data, 'euclidean');
link = linkage(dist, 'complete');
clust = cluster(link, 'maxclust', NumCluster);
color = link(end-NumCluster+2,3)-eps;
[H,T,perm] = dendrogram(link, 0, 'colorthreshold', color);
The above code will work for any values of "NumCluster" set to 2 or higher. The idea is to use the distance information returned by the LINKAGE function to identify a distance cut-off point such that coloring the clusters on the dendrogram plot below that point will result in the desired coloring effect. Since the distance information is returned in the third colomn of the "link" variable in ascending order, you can see that the value of "color" is set just below the line that would break the dendrogram plot into "NumCluster" clusters.
NOTE: The above code might not work well in situations with many repeated distance values returned in the "link" variable. This code is only provided as a guideline, and you should modify it as necessary to fit a given problem.
Best Answer