Registered Member
|
(but is it necessary?)
Ie i want to do the opposite of this: viewtopic.php?f=74&t=94839 the reason is that i want to use STL's nth_element function on the content of VectorXd... More precisely: i have a vector const VectorXd d = VectorXd::Random(n) and i want a vector VectorXi d_prime to contain the indexes of the m (1<m<<n) smallest entry of d. Best, |
Moderator
|
you can simply do that:
std::nth_element(d.data(), d.data()+n); |
Registered Member
|
sorry, but i don't understand:
double hst(int& n, int& m){ VectorXd d = VectorXd::Random(n); std::nth_element(d.data(),d.data()+n); return d(m); } gives a bug: error: no matching function for call to ‘nth_element(Eigen::PlainObjectBase<Eigen::Matrix<double, -0x00000000000000001, 1, 0, -0x00000000000000001, 1> >::Scalar*, Eigen::PlainObjectBase<Eigen::Matrix<double, -0x00000000000000001, 1, 0, -0x00000000000000001, 1> >::Scalar*)’ |
Moderator
|
I've been too fast, I only wanted to show you you can use raw pointer as iterators, so in your case this should be:
std::nth_element(d.data(),d.data()+m, d.data()+n); |
Registered Member
|
thank you very much sire,
so my code now is [simplifying a bit]: float hst(VectorXf& d,int& m){ // VectorXf d = VectorXf::Random(n); std::nth_element(d.data(),d.data()+m, d.data()+d.size()); return d(m); } then i: A) do a copy of d named d_2; B) feed d to hst(); C) compare all elements of d_2 to the return of hst() and set aside the indexes of the ones that are smaller unto a vector of int d_prim. Now, i) is this efficient (given that i won't need d/d_2 again latter)? ii) is there a recommended way to do A) and C) (assuming they are necessary)? Thanks in advance, |
Moderator
|
if m is not that small then the overhead of A) and C) should be totally negligible. An alternative would be to to use your own iterator storing references to both d and d_prim. d_prim would be initialized with 0 1 2 3 ... and then after call nth_element, d_prim.head(m) sill be the result you're looking for. This avoid A and C but add the initialization of d_prim and the nth_element step will be more costly because it will have to swap one float and one int every time instead of a single float. So overall I'm not sure at all that's worth the effort. Since I did something similar recently here is an exemple:
|
Registered Member
|
Ok thanks a lot,
PS: it's really a beginner's problem i guess, but for future reference [?] i place below the final version: VectorXi hst(VectorXf& d,int& h){ VectorXf e = d; VectorXi f(h); int j=0; std::nth_element(d.data(),d.data()+h,d.data()+d.size()); for(int i=0;i<d.size();i++){ if(e(i)<=d(h)){ f(j)=i; j++; } if(j==h) break; } return f; } |
Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]