我想对这个对称矩阵进行置换,如果我们将第二列移动到第三列,第二行也应该转到第三行。
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. ]])
这是列表上的排列代码:
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)
输出:
[[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]]
但是我希望对上面的数组进行排列,即4 * 4。为简单起见,如果我们有一个3 * 3阵列,我们想要下面的东西是K!= 6但是当k = 4时我们必须得到k!这是24个排列
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)
给出以下6个矩阵:
[[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]]
这是个问题吗?