MATLAB find() / Numpy nonzero idioms for Eigen

前端 未结 3 485
盖世英雄少女心
盖世英雄少女心 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:08

    Not sure if this is part of your question, but to construct the appropriate element-wise inequality result you must first cast your matrices to arrays:

    MatrixXd A,B;
    ...
    Matrix C = A.array()

    Now C is the same size as A and B and C(i,j) = A(i,j) < B(i,j).

    To find all of the indices (assuming column-major order) of the true entries, you can use this compact c++11 routine---as described by libigl's conversion table:

    VectorXi I = VectorXi::LinSpaced(C.size(),0,C.size()-1);
    I.conservativeResize(std::stable_partition(
      I.data(), I.data()+I.size(), [&C](int i){return C(i);})-I.data());
    

    Now I is C.nonZeros() long and contains indices of the true entries in C. These two lines essentially implement find.

提交回复
热议问题