There is a function VectorToSymmetricMatrix
that does the following:
Statistics`Library`VectorToSymmetricMatrix[{1, 2, 3}, {5, 6, 7}, 3] // MatrixForm
$$\left( \begin{array}{ccc} 5 & 1 & 2 \\ 1 & 6 & 3 \\ 2 & 3 & 7 \\ \end{array} \right)$$
I find it quite inconvenient to use because my data is differently ordered:
v={5, 1, 6, 2, 3, 7}
I am searching for a simple way to convert this vector to a symmetric matrix shown above.
There are similar posts, however, I could not find one addressing my problem. Please, try to provide a solution as simple and pedagogical as possible. I prefer to use documented functions even if the solution is slower.
A test case could be:
v = RandomReal[{0, 1}, {100, 75 (75 + 1)/2}];
m = Table[UnpackUpTrg[vi], {vi, v}];
My current implementation is
ind[i_, j_] := If[i < j, i + j (j - 1)/2, j + i (i - 1)/2]
UnpackUpTrg[v_] := Module[{k},
k = 1/2 (-1 + Sqrt[1 + 8 Length[v]]);
Table[v[[ind[i, j]]], {i, k}, {j, k}]
]
but it is a bit slow and procedural.
v1=v[[{2, 4, 5, 1, 3, 6}]]
gives he Order you want. andStatistics
LibraryVectorToSymmetricMatrix[v1[[1 ;; 3]], v1[[4 ;; 6]], 3]
creates the matrix. $\endgroup$