# How do I get the permutation matrix of an element in the symmetric group?

I have a permutation in $$S_4$$, Cycles[{2, 4}]. I want to produce the permutation matrix of this permutation. In other words, I want Mathematica to return the list {{1, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 1, 0, 0}}.

• This seem the Identy Matrix with the rows 2 and 4 interchanged. Table[IdentityMatrix[[i]],{i,PermutationList[Cycles[{{2, 4}}], 4]}] – vi pa Apr 13 '20 at 13:26

## 2 Answers

n = 4
SparseArray[
Transpose[{Range[n], PermutationList[Cycles[{{2, 4}}], n]}] -> 1,
{n, n}
]

• You don't even need to define n if you do SparseArray@MapIndexed[{#2[], #1} -> 1 &, PermutationList[Cycles[{{2, 4}}]]]. – Roman Apr 13 '20 at 12:22
• I know. Btw., calling SparseArray  and PermutationList without a second argument is asking for trouble. And Map and friends are slow. ;) – Henrik Schumacher Apr 13 '20 at 12:25

IdentityMatrix[[#]]&/@PermutationList[Cycles[{{2, 4}}], 4]

• Thank you. This is very helpful. – geoffrey Apr 13 '20 at 13:57
• IdentityMatrix[[PermutationList[Cycles[{{2, 4}}], 4]]] is a bit simpler. – Roman Apr 13 '20 at 13:58
• also: Permute[IdentityMatrix, Cycles[{{2, 4}}]] (+1) – kglr Apr 13 '20 at 16:43