Skip to content

Instantly share code, notes, and snippets.

@kersulis
Last active December 7, 2015 22:45
Show Gist options
  • Save kersulis/e503f884e44b9b5435ae to your computer and use it in GitHub Desktop.
Save kersulis/e503f884e44b9b5435ae to your computer and use it in GitHub Desktop.
Convert a permutation vector to its corresponding matrix.
"""
Given a permutation vector, return the corresponding permutation matrix such that
`A[p,:] = P*A`.
See [Wikipedia](https://en.wikipedia.org/wiki/Permutation_matrix#Definition).
"""
function pvec2mat(p)
n = length(p)
P = zeros(n,n)
for i in 1:n
P[i,:] = ej(n,p[i])
end
return sparse(P)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment