Skip to content

Instantly share code, notes, and snippets.

@mohanadkaleia
Created August 12, 2017 19:45
Show Gist options
  • Save mohanadkaleia/86a7d8e93e39a63122595c3aa8c61683 to your computer and use it in GitHub Desktop.
Save mohanadkaleia/86a7d8e93e39a63122595c3aa8c61683 to your computer and use it in GitHub Desktop.
Random and symmetric permutation matrix
import numpy as np
from itertools import product
def generatePermutationMatrix(n):
d = np.zeros((n, n))
index = np.random.permutation(range(n))
p = index[:n/2]
index1 = list(product(p, p))
for i in index1:
d[i] = 1
p = index[n/2:]
index2 = list(product(p, p))
for i in index2:
d[i] = 1
return d
@mohanadkaleia
Copy link
Author

Sample output

print generatePermutationMatrix(6)
[[ 1. 1. 0. 0. 0. 1.]
[ 1. 1. 0. 0. 0. 1.]
[ 0. 0. 1. 1. 1. 0.]
[ 0. 0. 1. 1. 1. 0.]
[ 0. 0. 1. 1. 1. 0.]
[ 1. 1. 0. 0. 0. 1.]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment