问题
If I have a matrix such as this:
A=[ 8 3 67 8 2 0 0 0 0 0; 8 76 9 233 1 89 34 0 0 0; 98 23 87 9 3 24 64 45 1 0]
and I use the command sort(A,2) it will sort alongside the columns but at the same time push the zeros forward. I would like to keep the 0's to the end for computing purposes later on in the code.
I I have been looking for a command that does this, as I feel there is one. I have also generally been trying to sort, flip, and transpose to get what I want but Im at an impasse.
回答1:
You can replace the 0
by Inf
, then sort and after the sort replace the Inf
by 0
once again.
来源:https://stackoverflow.com/questions/7871227/matlab-how-do-i-organize-rows-in-ascending-order-but-ignore-zero-values