Dear S'ers,
I'm dabbling with minimal spanning trees. I want the sum of the distances
for all branches of the tree in a neat form. As an example, I run mstree
with plan=F. This yields a vector of how the objects or cases connect
together. Say I have 18 objects, and mstree returns
5 15 11 6 10 12 4 9 13 3 18 11 16 13 4 17 5
Let this be stored in the vector mst. Now assume that I have also built a
distance (called dst) matrix for the L2 distances between each of the
objects (built with dist and translated to a matrix with dist2full). As I
understand this, the integrated distance in the Minimum Spanning Tree is
then the sum of dst[1, 5], dst[2, 15], dst[3, 6], ..., dst[17, 5]. I
naively thought that I could compute this as
sum(dst[1:17, mst])
but this returns the sum of the whole matrix, because dst[1:17, mst]
returns the whole matrix. I can do this in a for loop, but there is surely
an elegant, vector way to extract just these elements. What is it?
Kim Elmore
|