MATLAB find() / Numpy nonzero idioms for Eigen

前端 未结 3 440
盖世英雄少女心
盖世英雄少女心 2021-02-06 16:36

Chances are this is a very stupid question but I spent a pretty absurd amount of time looking for it on the documentation, to no avail.

in MATLAB, the find() function gi

3条回答
  •  别跟我提以往
    2021-02-06 17:12

    It is reasonable to expect Eigen to have a find() function. Unfortunately, Eigen doesn't have one, or even a less than operator for matrices. Fortunately, the problem isn't too difficult. Here is one solution to the problem. I am using vector to store the Column Major indices of elements > 0. You could use VectorXf if you prefer that. Use this on B - A (B-A > 0 is the same as evaluating B>A). I'm using the stl for_each() function.

    #include
    #include
    #include 
    using namespace Eigen;
    using namespace std;
    
    class isGreater{
        public:
        vector* GT;
        isGreater(vector *g){GT = g;}
        void operator()(float i){static int it = 0; if(i>0)GT->push_back(it); it++;}
    };
    int main(int argc,char **argv){
        MatrixXf P = MatrixXf::Random(4,5);
        vector GT;
        for_each(P.data(),P.data()+P.rows()*P.cols(),isGreater(>));
        cout<

提交回复
热议问题