I have a matrix in numpy, i.e.NxM ndarray, which looks like this:
[
[ 0, 5, 11, 22, 0, 0, 11, 22],
[ 1, 4, 11, 20, 0, 4, 11, 20],
[ 1, 6, 11, 22, 0, 1, 11, 22],
[ 4, 7, 12, 21, 0, 4, 12, 21],
[ 5, 7, 12, 22, 0, 7, 12, 22],
[ 5, 7, 12, 22, 0, 5, 12, 22]
]
I would like to sort it by lines, putting zeros in each line first, without changing the order of the remaining elements along the line.
My desired result is as follows:
[
[ 0, 0, 0, 5, 11, 22, 11, 22],
[ 0, 1, 4, 11, 20, 4, 11, 20],
[ 0, 1, 6, 11, 22, 1, 11, 22],
[ 0, 4, 7, 12, 21, 4, 12, 21],
[ 0, 5, 7, 12, 22, 7, 12, 22],
[ 0, 5, 7, 12, 22, 5, 12, 22]
]
For efficiency, I have to do this with numpy (therefore, it is not recommended to use regular Python nested lists and do calculations on them). The faster the code, the better.
How can i do this?
Best, Andrea