I am interested in generating a 1D list of the upper-triangle elements of a matrix.
I can grab the indices of these elements by doing:
upperTrianglularIndices[dim_] := Subsets[Table[i, {i, val /. val -> dim}], {2}];
But I'm struggling to actually select the matrix elements using these indices.
If I have a function, then I know I can map-thread it using this code:
f @@ # & /@ upperTrianglularIndices[3]
which will return:
{f[1, 2], f[1, 3], f[2, 3]}
so my idea is to express my matrix as a function, function[matrix[[x]],x], and then use the above code to apply it to each index. I see that this works correctly like this:
(x \[Function] f[[x]]) @@ # & /@ upperTrianglularIndices[3]
But doesn't work when I specify that I want the function to be the selection of the matrix element:
(x \[Function] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}[[x]]) @@ # & /@
upperTrianglularIndices[3]
This returns (with some errors):
{f[[1]], f[[1]], f[[2]]}
What is the problem here?
Statistics`Library`UpperTriangularMatrixToVector[f]
$\endgroup$