Ask Your Question

How can I sort Mat with col order

asked 2015-01-27 22:42:21 -0500

dinl gravatar image

I have a Mat element, that I want to sort with col oder, so for example I have:

Mat A ={[2 1], [0 2], [1 2], [0 1]}

So, If I use:


They will order each column independently, but the expected result is to order the second column first, then if a draw happens it will sort by the first column:


{[0 1], [0 1], [1 2], [2 2]}


{[0 1], [2 1], [0 2], [1 2]}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-01-28 14:08:50 -0500

Guanta gravatar image

The result of OpenCV is absolutely correct! Each column is sorted individually.

If you want to sort by the second column you need to use cv::sortIdx() on the second column only ( you can use .col(1)) and then use these indices, i.e. create a new matrix and copy each row from A to the new matrix according to this index.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-01-27 22:42:21 -0500

Seen: 2,763 times

Last updated: Jan 28 '15