Custom Dendrogram Ordering Implementation

4 次查看(过去 30 天)
Hi,I'm trying to implement a custom ordering of a dendrogram (obtained using the linkage function) based on the rule that the "highest" branch has to be always on the right side. As an example:
%generate a tree using linkage
rng('default'); %for reproducibility
X = rand(10,2);
D = pdist(X);
tree = linkage(D,'average');
%my desired order
myOrd = [1,4,5,8,9,2,10,3,6,7];
%plot
figure();
subplot(2,1,1);
dendrogram(tree);
title('Default leaf order');
subplot(2,1,2);
dendrogram(tree,'Reorder',myOrd);
title('Desired leaf order');
Do you have any idea for a more efficient implementation?
Thank you

采纳的回答

Massimo Zanetti
Massimo Zanetti 2016-12-16
Apparently, none of the available functionalities of optimalleaforder does the work. So here it is a function that runs through the rows of the tree matrix generated by linkage and produces your output.
function c = binOrd(tree)
p = size(tree,1);
c = zeros(1,p+1);
k = 0;
recBin(p);
function recBin(r)
x = tree(r,1:2)-p-1;
if x(1)<=0
c(k+1) = tree(r,1);
k = k+1;
else
recBin(x(1));
end
if x(2)<=0
c(k+1) = tree(r,2);
k = k+1;
else
recBin(x(2));
end
end
end
To get your desired rightmost binary ordering, run dendogram with 'Reorder' set as the output of the binOrd function:
tree = linkage(D,'average');
myBinOrder = binOrd(tree);
dendrogram(tree,'Reorder',myBinOrder);
  1 个评论
Daniele
Daniele 2016-12-16
Thank you, that is exactly what I need and it is super-fast. Recursive functions are really powerfull.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by