# How to remove the diagonal of a matrix

I just don't need to extract the diagonal of a matrix, but I need to remove it so if I have a matrix of size $$N\times N$$:

$$\begin{bmatrix} x_{11} & x_{12} & x_{13} \\ x_{21} & x_{22} & x_{23} \\ x_{31} & x_{32} & x_{33} \\ \end{bmatrix}$$

I end up with a matrix of size $$N\times(N-1)$$:

$$\begin{bmatrix} x_{12} & x_{13} \\ x_{21} & x_{23} \\ x_{31} & x_{32} \\ \end{bmatrix}$$

If it is useful, in my case all the rows of the matrix are equal, so the question can be also how to transform a vector: $$\begin{matrix} [ x_{1} & x_{2} & x_{3} ] \end{matrix}$$

Into the matrix:

$$\begin{bmatrix} x_{2} & x_{3} \\ x_{1} & x_{3} \\ x_{1} & x_{2} \\ \end{bmatrix}$$

Any matrix mapping or help is really appreciated! Thanks a lot!

• If you encode $M\times N$ matrices as $MN$-vectors, then of course you can obtain a $N(N-1)\times N^2$ matrix. I don't know what else you are envisioning. Apr 29, 2021 at 16:23
• @Javier Could gives us some more context? What do you mean by "you need to remove the diagonal"? Do you need to tell a computer to remove the diagonal from a matrix? Do you need to say in a mathematical manuscript that you are removing the diagonal? Are you trying to remove the diagonal using only a certain set of allowed operations? Apr 29, 2021 at 16:24
• @BenGrossmann I need to represent that operation in a mathematical way, so I can work with that expression later in future operations. My first idea was trying to find a linear transformation matrix trying to solve the system of equations to obtain the result, but there is none. Apr 29, 2021 at 16:33
• @JavierGermánLuzón You have already "represented that operation in a mathematical way". One way to formally describe the operation is like this: we define $f:\Bbb R^{N \times N} \to \Bbb R^{N \times (N-1)}$ so that if $x_{ij}$ denotes the $i,j$ entry of $X$, $f(X)$ is the matrix whose $i$th row is $$(x_{i,1},\dots,x_{i,i-1},x_{i,i+1},\dots,x_{i,N}).$$ Apr 29, 2021 at 16:53
• @BenGrossmann sorry for not being clear. What I need is a linear transformation of the matrix in order to obtain a reduced matrix without the diagonal elements. Apr 29, 2021 at 17:09

You can always select an arbitrary element of a matrix by multiplying by the row number matrix from the left and the column number matrix from the right (these matrices have the form $$diag(0,0...0,1,0...0)$$ with one on the spot of the row/column entry). Example for extracting the element in the first row, second column: $$\pmatrix{1 & 0 & 0\\ 0 & 0 & 0\\ 0 & 0 & 0}\pmatrix{a & b & c\\ d & e & f\\ g & h & i}\pmatrix{0 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 0} = \pmatrix{0& b & 0\\ 0 & 0 & 0\\ 0 & 0 & 0}.$$ So add up the non diagonal elements and you will get the matrix with zeroes on the diagonal as follows: $$\pmatrix{0& b & c\\ d & 0 & f\\ g & h & 0}.$$ This should already bring you closer to what you need.

For your second scenario you can "tile" the vector such that you get an $$N\times N$$ matrix with $$N$$ copies of the row vector in question.

Inspiration from: Other Question

If you take the vector as a $$1\times 3$$ matrix $$v = \begin{pmatrix} x_1 & x_2 & x_3 \end{pmatrix}$$ then

$$\begin{pmatrix} x_2 & x_3 \\ x_1 & x_3 \\ x_1 & x_2 \end{pmatrix} = \begin{pmatrix} 1 \\ 0 \\ 0 \end{pmatrix} v \begin{pmatrix} 0 & 0 \\ 1 & 0 \\ 0 & 1 \end{pmatrix} +\begin{pmatrix} 0 \\ 1 \\ 0 \end{pmatrix} v \begin{pmatrix} 1 & 0 \\ 0 & 0 \\ 0 & 1 \end{pmatrix} +\begin{pmatrix} 0 \\ 0 \\ 1 \end{pmatrix} v \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 0 & 0 \end{pmatrix}$$

But I doubt this turn out to be useful. I thing you'll do better just using the matrix you want instead of a representation through matrix operations.

• Thanks a lot anyway, the problem I have is that I have to use this matrix in two studies, one experimental (using Matlab) and one analytical and then compare both. Removing the diagonal is easy in Matlab, but for the analytical part I cannot find the way to make it an easy transformation. I need to remove it because I have to make the logarithm of the elements of the matrix and my diagonal values are zero, so I need to remove them. Apr 29, 2021 at 17:28
• Hi, quick note: the matrix product of the column vector $\begin{bmatrix}1\\0\\0\end{bmatrix}*\begin{bmatrix}x_1&x_2&x_3\end{bmatrix}=\begin{bmatrix}x_1&x_2&x_3\\0&0&0\\0&0&0\end{bmatrix}$ and is a square matrix. Should the formula have $v$ and the unit column matricies be commuted? Sep 27, 2022 at 22:34
• @Gerald That square matrix is then multiplied by a $3 \times 2$ matrix, so all the dimensions are consistent. Sep 27, 2022 at 23:13