# Generalizing permutation matrices

I'm working on LU factorization that involves pivoting rows and I'm still trying to work my head around how to obtain the permutation matrices with ease.

Say I want to interchange two rows. If I have something as simple as $$A = \begin{bmatrix} a & b \\ c & d \end{bmatrix}$$ then I can left multiply by $$P = \begin{bmatrix} p_1 & p_2 \\ p_3 & p_4 \end{bmatrix}$$, form a system under these matrices so that Gaussian elimination gives me $$P = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}$$, satisfying $$\begin{equation*} \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} a & b \\ c & d \end{bmatrix} = \begin{bmatrix} c & d \\ a & b \end{bmatrix} \end{equation*}$$

But how do I do this for larger matrices between any arbitrary rows? Is there a fast algorithm that allows me to do this?

• Take the identity matrix then swap the rows you want to permute. It's that easy. Commented Nov 14, 2022 at 19:48
• @CyclotomicField, I've tired that with $A =[8 \ \ 7 \ \ 3; 4 \ \ 1 \ \ 2; 6\ \ -1\ \ 3]\in\mathbb R^{3\times 3}$, swapping rows 2 and 3 by $P = [1 \ \ 0 \ \ 0; 0 \ \ 1 \ \ 0; 1 \ \ 0 \ \ 0]$ but it doesn't get me the desired result. I think the issue is swapping the row $[0 1 0]$ which goes unchanged
– user1092432
Commented Nov 14, 2022 at 21:46

We say that $$P$$ is a permutation matrix if any row and any column have exactly one element equals 1 while the others are equals 0. Take, for example: $$P = \begin{bmatrix} 1&0&0\\0&0&1\\0&1&0\end{bmatrix}$$
Let $$A$$ be a $$3\times 3$$ matrix. If $$PA = A'$$, $$A'$$ will be equal the following row swapping of $$A$$:
• The first row of $$A'$$ is equal the first row of $$A$$, because the first row of $$P$$ has $$1$$ in the 1st column.
• The second row of $$A'$$ is equal the third row of $$A$$, because the second row of $$P$$ has $$1$$ in the 3rd column.
• The third row of $$A'$$ is equal the second row of $$A$$, because the second row of $$P$$ has $$1$$ in the 2rd column.