I think using np.unravel_index
be one of the fastest way besides the following advanced indexing to swap the rows:
row_min = np.unravel_index(np.argmin(mat), mat.shape)[0]
row_max = np.unravel_index(np.argmax(mat), mat.shape)[0]
mat[[row_min, row_max]] = mat[[row_max, row_min]]
Benchmarks (Colab):
# matrix (3*3) FASTEST
1000 loops, best of 5: 8.7 µs per loop ------> hilberts_drinking_problem method
1000 loops, best of 5: 14.3 µs per loop
# matrix (1000*3)
100 loops, best of 5: 65 µs per loop
100 loops, best of 5: 21.9 µs per loop ------> This method
# matrix (1000*1000)
100 loops, best of 5: 3.44 ms per loop
100 loops, best of 5: 2.64 ms per loop ------> This method
# matrix (10000*10000)
10 loops, best of 5: 388 ms per loop
10 loops, best of 5: 282 ms per loop ------> This method