Created
August 12, 2017 19:45
-
-
Save mohanadkaleia/86a7d8e93e39a63122595c3aa8c61683 to your computer and use it in GitHub Desktop.
Random and symmetric permutation 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.]]