Last active
December 7, 2015 22:45
-
-
Save kersulis/e503f884e44b9b5435ae to your computer and use it in GitHub Desktop.
Convert a permutation vector to its corresponding matrix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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