1

I want to have the permutation for this symmetric matrix, if we move the the second column to the third the second row also should go to the third row.

array([[ 0.        ,  0.06377803,  0.1157737 ,  0.19542195],
       [ 0.06377803,  0.        ,  0.14754803,  0.23185761],
       [ 0.1157737 ,  0.14754803,  0.        ,  0.0843134 ],
       [ 0.19542195,  0.23185761,  0.0843134 ,  0.        ]])

This is code for permutation on a list:

import numpy as np
x=[]
def perm(a, k=0):

    if k == len(a):
        x.extend(a)
#        print (a )


    else:
      for i in range(k, len(a)):
         a[k], a[i] = a[i] ,a[k]
         perm(a, k+1)
         a[k], a[i] = a[i], a[k]

perm([0,1,2,3])
a=np.asarray(x).reshape((24,4))
print(a)

Output:

[[0 1 2 3]
 [0 1 3 2]
 [0 2 1 3]
 [0 2 3 1]
 [0 3 2 1]
 [0 3 1 2]
 [1 0 2 3]
 [1 0 3 2]
 [1 2 0 3]
 [1 2 3 0]
 [1 3 2 0]
 [1 3 0 2]
 [2 1 0 3]
 [2 1 3 0]
 [2 0 1 3]
 [2 0 3 1]
 [2 3 0 1]
 [2 3 1 0]
 [3 1 2 0]
 [3 1 0 2]
 [3 2 1 0]
 [3 2 0 1]
 [3 0 2 1]
 [3 0 1 2]]

But I want to have the permutation for the above array which is 4*4. For simplicity if we have a 3*3 array, we want something like below which is K!=6 but when k=4 then we have to get k! which is 24 permutations

enter image description here

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
user8523104
  • 467
  • 1
  • 3
  • 14

1 Answers1

3
import numpy as np
from itertools import permutations

n = 3
a = np.arange(n**2).reshape(n, n)

for perm in permutations(range(a.shape[0])):
    b = np.zeros_like(a)
    b[:, :] = a[perm, :]
    b[:, :] = b[:, perm]
    print(b)

gives the 6 following matrices:

[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[0 2 1]
 [6 8 7]
 [3 5 4]]
[[4 3 5]
 [1 0 2]
 [7 6 8]]
[[4 5 3]
 [7 8 6]
 [1 2 0]]
[[8 6 7]
 [2 0 1]
 [5 3 4]]
[[8 7 6]
 [5 4 3]
 [2 1 0]]

Is this the question?

xdze2
  • 3,986
  • 2
  • 12
  • 29