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

8
  • Are you saying you want to permute the matrix in such a way that it status symmetrical? Commented Sep 3, 2018 at 20:28
  • Fix your indentation and use a return value instead of a global. Commented Sep 3, 2018 at 20:29
  • Fix your indentation Commented Sep 3, 2018 at 20:47
  • Thank you for you comment, if you look at the image you will see there is no problem for diagonal and off-diagonal elements because the diagonal still is zero, Commented Sep 3, 2018 at 20:54
  • 1
    I think the image is really clear what I want to get, permutation for the above array vertically and horizontally please look the image to see what I wan to have Commented Sep 3, 2018 at 21:19

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.