I have a matrix that should have ones on the diagonal but the columns are mixed up.

But I don’t know how, without the obvious for loop, to efficiently interchange rows to get unity on the diagonals. I’m not even sure what key I would pass to sort on.
Any suggestions?
You can use numpy’s
argmaxto determine the goal column ordering and reorder your matrix using the argmax results as column indices: