Given an upper triangular matrix (or lower triangular)
A = np.array([[1, 2, 3],
[0, 4, 5],
[0, 0, 6]]
)
I would like to sort its values from largest to smallest, keeping track of the ij indices, so for instance in this matrix I would want something like
6, (2,2)
5, (1,2)
4, (1,1)
3, (0,2)
2, (0,1)
1, (0,0)
This simple example uses a matrix with integers, but it should handle floats as well as arbitrarily large matrices, generally between 10x10 and 40x40. Speed is not very important, it is a one time operation. Also, it need not be upper or lower, the matrix is symmetrical, so I can simply fill in the opposite triangle if that is easier.
I have tried np.argsort()
but that just gives
array([[0, 1, 2],
[0, 1, 2],
[0, 1, 2]], dtype=int64)
which is to say, it sorts each row independently. This doesn't help much since I don't want a list of the largest elements in each row, it could be that one row has all of the largest values.