Let us say that I have a $3 \times 3$ matrix $\bf X$, that has to be reshaped into a vector and rearranged as follows:

$$ {\bf v} ({\bf X}) = \begin{bmatrix} x_{22} & (x_{23}+x_{32}) & (x_{21}+x_{12}) & x_{33} & (x_{13}+x_{31}) & x_{11} \end{bmatrix} $$

where the indices indicate the position of the elements in the matrix $\bf X$. How to do this rearrangement automatically for any size of matrix $\bf X$?

The ${\bf v} ({\bf X})$ represents the coefficients of monomials of a vector $y = \begin{bmatrix} a_1 & a_2 & b\end{bmatrix}^T$ of degree 2 i.e. the elements of ${\bf v} ({\bf X})$ are coefficients of $z = \begin{bmatrix} a_1^2 & a_1a_2 & a_1b & a_2^2 & a_2b & b^2 \end{bmatrix}^T$. So, the matrix ${\bf X}$ contains coefficients of z in a fashion that, the diagonal elements are monomials without cross product and the off-diagonal elements are monomials with two variable.

$$ {\bf X} = \operatorname{coeff} \left( \begin{bmatrix} b^2 & \frac{a_1b}{2} & \frac{a_2b}{2} // \frac{a_1b}{2} & a_1^2 & \frac{a_1a_2}{2} // \frac{a_2b}{2} & \frac{a_1a_2}{2} & a_2^2\end{bmatrix} \right).$$

For $y = \begin{bmatrix} a_1 & a_2 & a_3 & b\end{bmatrix}^T$ and degree 2, $z = \begin{bmatrix} a_1^2 & a_1a_2 & a_1a_3 & a_1b & a_2^2 & a_2a_3 & a_2b & a_3^2 & a_3b & b^2 \end{bmatrix}^T$ and the matrix ${\bf X}$ will be of size $4 \times 4$.

  • $\begingroup$ I edited my question further for clarity. The size of the matrix $\bf X$ is not always the same. So the rearrangement of elements depend on this size. Also, yes, it is a symmetric matrix and the off-diagonal elements appear in pairs. I explicitly mentioned it for clarity. I am not sure how to rearrange the elements. I tried with permuting all elements indices, but I dont know how to reorder them in the way i desire. $\endgroup$
    – Neuling
    May 23, 2023 at 7:11
  • $\begingroup$ The matrix is symmetric only up to certain decimal and the symmetricity is depending on the noise in the data. The problem, I am trying to solve is from signal processing and system identification, where the data contains noise. In no noise case, it is safe to say they are symmetric. If that is confusing, I will remove the symmetric matrix in the question $\endgroup$
    – Neuling
    May 23, 2023 at 7:50
  • 3
    $\begingroup$ You complicate things. Assume that the input matrix is symmetric and find the desired reshaping operator. If the input matrix is almost symmetric but not symmetric, then make it symmetric by $${\bf X} \mapsto \dfrac{{\bf X} + {\bf X}^\top}{2}$$ $\endgroup$ May 23, 2023 at 7:59

1 Answer 1


$${\bf X} \mapsto \left( {\bf P} \operatorname{vech} ({\bf X}) \right)^\top$$

where $\operatorname{vech} : {\Bbb S}_n (\Bbb R) \to {\Bbb R}^m$, where ${\Bbb S}_n (\Bbb R)$ is the set of $n \times n$ real symmetric matrixes and $m := \binom{n+1}{2}$, is the half-vectorization operator and $\bf P$ is a $m \times m$ permutation matrix.

  • $\begingroup$ The question here is to find the permutation matrix ${\bf P}$. For different sizes of ${\bf X}$, the ${\bf P}$ changes. Also, please see the correction in the question about the ${\bf v(X)}$. The off diagonal elements need to be just summed and not averaged. Then for the symmetric matrix, the corresponding element in ${\bf P}$ would be 2, instead of 1. $\endgroup$
    – Neuling
    May 23, 2023 at 13:40
  • $\begingroup$ @Neuling You changed your question. I am under no obligation to adapt to your bad behaviour. $\endgroup$ May 23, 2023 at 14:25
  • $\begingroup$ Thank you for your time. The question I initially posted was a mistake and I didn't notice it. As soon as I did, I edited it. This isn't a bad behaviour, but just a human error :) $\endgroup$
    – Neuling
    May 23, 2023 at 15:53
  • $\begingroup$ @Neuling If you orphaned my answer, that is bad behaviour. If you wanted to ask a different question, then you could have asked a new question and linked to this one $\endgroup$ May 23, 2023 at 16:07
  • $\begingroup$ Alright, thank you. As a new user, I am not aware of this. I will post a new question. Thanks again $\endgroup$
    – Neuling
    May 23, 2023 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.