Registered Member
|
Hi,
i have the following Matlab statement I have to implement efficiently using Eigen:
Basically it takes a row from the matrix D and saves it in d. Then it sorts d, but I only need the indexes of the sorting. Then I save the first 5 sort indices in another matrix. I have a solution but that is realy slow:
sort_vector implements insertion sort on both d and sort_ind. I have to perform this operation a few thousand times, is there any better solution to this problem? |
Registered Member
|
To sort a Eigen vector, use STL algorithms. You need a pointer to the start of the vector's coeffs, use .data() to get it.
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
Registered Member
|
hm OK I have no idea if that gives you a good way to sort both the vector and the indices at the same time, to be efficient. Maybe you'll have to define your own element type consisting of a (float,int) pair, and use STL algorithm on that.
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
Registered Member
|
ok, I try to use now something similar to this:
now my remaining question is how to efficiently fill the vector<matrix_index> and after sorting get the indices as VectorXi so that i can use .head(5). The indices should start at 0 and go up to size(). Currently i use a for loop, but is there a more direct conversion to a std:vector? From the resulting sorted vector back to VectorXi seems not to be so much important as I am going to take only the first 5 elements anyway... |
Registered Member
|
Here is what usually do. First you define your own compare functor using index
Then you define another index vector, then sort it as
Sort vector of Eigen is similar, you just pass the pointer of the internal data to the compare functor. |
Registered Member
|
It occured to me that since you only want the top 5 entries, you should just apply the trivial algorithm of looking for the first one, then for the second one, etc. You will be done in 5*n=O(n) operations, compare to the O(n*log(n)) operations needed by sort algorithms.
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
Registered Member
|
That's right. Instead using std::sort, he can use the std::nth_element algorithm which can return top k elements without ordering in O(n) which is irrelevant to k. |
Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient