What's the best way to do the following in Numpy when dealing with symmetric square matrices (NxN
) where N > 20000
?
>>> a = np.arange(9).reshape([3,3])
>>> a = np.maximum(a, a.T)
>>> a
array([[0, 3, 6],
[3, 4, 7],
[6, 7, 8]])
>>> perm = np.random.permutation(3)
>>> perm
array([1, 0, 2])
>>> shuffled_arr = a[perm, :][:, perm]
>>> shuffled_arr
array([[4, 3, 7],
[3, 0, 6],
[7, 6, 8]])
This takes about 6-7 secs when N is about 19K. While the same opertation in Matlab takes less than a second:
perm = randperm(N);
shuffled_arr = arr(perm, perm);