Reputation: 1365
If I have a square and symmetric matrix, for example,
[[0 3 2]
[3 8 4]
[2 4 5]]
I do not want to shuffle rows only or columns only. instead,
how can I, for example (not the following in the strict order as written, but instead at random):
Upvotes: 2
Views: 217
Reputation: 150735
What you are asking for can be done with so-called matrix conjugation:
perm_mat = np.random.permutation(np.eye(len(a),dtype=np.int))
out = (perm_mat @ a) @ (np.linalg.inv(perm_mat))
Output (random of course):
array([[8., 4., 3.],
[4., 5., 2.],
[3., 2., 0.]])
Or can be done with slicing:
np.random.seed(1)
orders = np.random.permutation(np.arange(len(a)))
a[orders][:,orders]
Output:
array([[0, 2, 3],
[2, 5, 4],
[3, 4, 8]])
Upvotes: 2