简体   繁体   English

具有特征矩阵的 Stl 迭代器

[英]Stl iterators with Eigen matrices

I have some functions that involves stl iterators and works with types like std::vector<Eigen::Vector2d> .我有一些涉及 stl 迭代器的函数,并适用于std::vector<Eigen::Vector2d>等类型。 For example:例如:

template<typename T>
bool isLeftOf(const Eigen::Vector2<T>& a,
              const Eigen::Vector2<T>& b) {
  return (a.x() < b.x() || (a.x() == b.x() && a.y() < b.y()));
}

int main()
{
  std::vector<Eigen::Vector2i> myVec;
  myVec.push_back(Eigen::Vector2i::Random(2));
  myVec.push_back(Eigen::Vector2i::Random(2));
  myVec.push_back(Eigen::Vector2i::Random(2));
  myVec.push_back(Eigen::Vector2i::Random(2));
  myVec.push_back(Eigen::Vector2i::Random(2));
  
  Eigen::Vector2i element = *std::min_element(myVec.begin(), myVec.end(), isLeftOf<int>);

  return 0;
}

As you can see I create std::vector<Eigen::Vector2i> myVec and work with Eigen::Vector2<T> in function isLeftOf when calling std::min_element .如您所见,我在调用std::min_element时创建了std::vector<Eigen::Vector2i> myVec并在 function isLeftOf中使用Eigen::Vector2<T>

Now I'm experincing some troubles while working with std::vector<SomeEigenType> and I'm looking for a way to work with the same isLeftOf(Eigen::Vector2...) and stl functions but I don't understand how.现在我在使用std::vector<SomeEigenType>时遇到了一些麻烦,我正在寻找一种方法来使用相同的isLeftOf(Eigen::Vector2...)和 stl 函数,但我不明白如何.

In Eigen documentation or in the forum there is some information how to perform stl operations on Eigen::Vector or Matrix but they work with plain numbers of matrix so I cant send Eigen::Vector2 to my isLeftOf function.Eigen 文档论坛中,有一些信息如何在 Eigen::Vector 或 Matrix 上执行 stl 操作,但它们使用普通数字矩阵,因此我无法将Eigen::Vector2发送到我的isLeftOf function。

Is there a way to use stl functions with Eigen::Matrix and process condition in my functions like isLeftOf that accept Eigen::Vector types?有没有办法在我的函数中使用 stl 函数和Eigen::Matrix和过程条件,比如isLeftOf接受Eigen::Vector类型?

Using the master version of Eigen, you can use STL-like iterators which access rows or columns of matrices:使用 Eigen 的主版本,您可以使用类似 STL 的迭代器来访问矩阵的行或列:

Assuming myVec is a matrix you can write this to get the "left-most" column:假设myVec是一个矩阵,你可以这样写来获得“最左边”的列:

  Eigen::Vector2i element = *std::min_element(
      myVec.colwise().begin(), myVec.colwise().end(), 
      [](auto const& a, auto const& b){
          return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
      });

Godbolt-Demo: https://godbolt.org/z/n1Y8hf Godbolt 演示: https://godbolt.org/z/n1Y8hf

I have not found the best solution but for now I decided to use lambda functions:我还没有找到最好的解决方案,但现在我决定使用 lambda 函数:

#include <algorithm>
#include <Eigen/Dense>

int main()
{
  Eigen::MatrixX2f M;

  /*
   *  fill matrix M as you need
   * */
  
  // initialize original index locations from 0 to N-1
  Eigen::VectorX<Eigen::Index> idx =
      Eigen::ArrayX<Eigen::Index>::LinSpaced(
        M.rows(), 0, M.rows()-1);

  std::function<bool(const Eigen::Index &, const Eigen::Index &)> isLeftOf_fun = 
      [&M](
      const Eigen::Index& row1,
      const Eigen::Index& row2)->bool
  {
    return (M(row1,0) < M(row2,0) || (M(row1,0) == M(row2,0) && M(row1,1) < M(row2,1)));
  };

  Eigen::Index ia = *min_element(idx.begin(), idx.end(), isLeftOf_fun);
  Eigen::Vector2f element = M.row(ia);

  return 0;
}

The main idea is to apply stl iterator on index vector wich has values from 0 to N-1 (where N is the number of rows of matrix) and process the chosen rows in lambda (or in standard) function.主要思想是在索引向量上应用 stl 迭代器,其值从0N-1 (其中N是矩阵的行数)并处理 lambda(或标准)ZC1C425268E683849D1AB47A 中选择的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM