Skip to content

Instantly share code, notes, and snippets.

@rhngla
Created July 8, 2020 19:31
Show Gist options
  • Save rhngla/e5df45c52ce77c66fe0cb5278f651fcb to your computer and use it in GitHub Desktop.
Save rhngla/e5df45c52ce77c66fe0cb5278f651fcb to your computer and use it in GitHub Desktop.
Calculate tree lengths for trees in the graph specified by (AM,r)
function tree_lengths=TreeLengths(AM,r)
%Returns total length measured for each of the L trees defined by the labeled adjacency matrix.
%Inputs:
% AM : (NxN) matrix labeled adjacency matrix (assumed symmetric)
% r : (Nx3) position vector for the N nodes
%Outputs:
% tree_lengths : (1xL)
AMlbl_direct=triu(AM,1);
tree_labels=unique(AMlbl_direct(AMlbl_direct>0));
tree_lengths=zeros(1,max(tree_labels));
for l=1:length(tree_labels)
[ii, jj]=find(AMlbl==tree_labels(l));
tree_lengths(tree_labels(l))=sum(sum((r(ii,:)-r(jj,:)).^2,2).^0.5)/2;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment