# Symmetrically permute a matrix

I’d like to symmetrically permute a matrix. For example, I want to transform

\left[ \begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9\end{array}\right]

by rearranging the rows and columns according to the permuation [ 3, 1, 2 ], to get

\left[ \begin{array}{ccc} 9 & 7 & 8 \\ 3 & 1 & 2 \\ 7 & 4 & 5\end{array}\right]

In MATLAB I’d do

p = [3 1 2]
A = [1 2 3; 4 5 6; 7 8 9]
A_permuted = A(p, p)


How do I do it with Julia?

The (almost) exact same code:

p = [3, 1, 2] # comma separated so this is a vector as opposed to a 1x3 Matrix
A = [1 2 3; 4 5 6; 7 8 9]
A_permuted = A[p, p]

6 Likes