Boost logo

Ublas :

Subject: Re: [ublas] How to sort rows/columns in sparse matrix
From: Ernest Turro (ernest.turro_at_[hidden])
Date: 2010-03-05 17:12:24


On 5 Mar 2010, at 16:27, Ernest Turro wrote:

> Hi,
>
> I'd like to sort the rows of my row-major compressed_matrix. Ideally I would use standard sort(), passing mat.begin1(), mat.end1() and a compare function. The compare function would take two matrix_row<M> parameters, use them to compare the two rows by iterating over the non-zero elements and return a boolean. However, this results in plenty of compiler errors relating to various operands not being implemented for the two iterators (-, +, <) (cf. http://pastebin.com/G04HAJSf ). Does anyone have any suggestions on how to achieve this? I imagine it would be quite useful in general, e.g. to remove duplicate rows from a sparse matrix.

Further to my last message, I'd like to share the solution I've found:

Say we have a nxm matrix.
Create a vector of integers 0,1,2,...., n-1
Use C++ sort to sort the integers, passing a compare function which compares the rows of the matrix indicated by the integers (so, alas, the matrix has to be global and declared before the compare function declaration)
Construct a new matrix with the row orderings indicated by the sorted vector.

>
> Regards,
> Ernest
>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: ernest.turro_at_[hidden]