Constructing a permutation matrix

Using an explicit multiplication PA of dense matrices to permute the rows of A transforms the cost from O(n^2) to O(n^3) for an n \times n matrix.

But if you do want a permutation matrix, it’s much easier (and faster) to simply do

using LinearAlgebra
MP(p) = I(length(p))[p,:]
6 Likes