Suppose a numpy array A
with shape (n,)
, and a boolean numpy matrix B
with shape (n,n)
.
If B[i][j]
is True
, then A[i]
should be sorted to a position before A[j]
.
If B[i][j]
is False
, then A[i]
should be sorted to a position after A[j]
.
These rules are applicable only if B[i][j]
is below the main diagonal. Elements on the main diagonal or above the main diagonal should be ignored.
That being said, what is the most efficient way to sort A
according to the matrix B
?
I know there are several easy ways to do this, but I must perform this operation thousands of times, so I'm looking for a way to implement this that is computationally efficient ( readability is not my main concern ).