1

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

Neuling
  • 23
  • 6
  • 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. – Neuling May 23 '23 at 07: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 – Neuling May 23 '23 at 07:50
  • 3
    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}$$ – Rodrigo de Azevedo May 23 '23 at 07:59

1 Answers1

2

$${\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. – Neuling May 23 '23 at 13:40
  • @Neuling You changed your question. I am under no obligation to adapt to your bad behaviour. – Rodrigo de Azevedo May 23 '23 at 14:25
  • 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 :) – Neuling May 23 '23 at 15:53
  • @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 – Rodrigo de Azevedo May 23 '23 at 16:07
  • Alright, thank you. As a new user, I am not aware of this. I will post a new question. Thanks again – Neuling May 23 '23 at 16:10
  • @Neuling Focus on the smallest question that makes sense. And stay tethered. Do you really need to remove the averages? What you have now is the input matrix $$\frac{{\bf X} + {\bf X}^\top}{2} - \mbox{diag}({\bf X})$$ Why don't you stick to symmetric matrices? If a matrix is not symmetric, you can always extract its symmetric part – Rodrigo de Azevedo May 23 '23 at 16:18