# Reshaping a matrix and rearranging the elements lexicographically into a vector

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$$.

• 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. May 23 at 7:11
• 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 May 23 at 7:50
• 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}$$ May 23 at 7:59

$${\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.
• 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. May 23 at 13:40