Custom Dendrogram Ordering Implementation

조회 수: 4 (최근 30일)
Daniele
Daniele 2016년 12월 16일
댓글: Daniele 2016년 12월 16일
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에서 Point Cloud Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by