MatLab, How do I organize rows in ascending order but ignore zero values?

谁都会走 提交于 2019-12-11 04:31:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!