Search code examples
matrixiteratoreigeneigen3

Is there a line iteration for Eigen matrix so that I can iterate through the matrix line by line?


The question is about Eigen. Being used to iterator in C++, I think it is natural to expect that there is an line (or column) iterator for Eigen matrix so that I can iterate through the matrix line by line, e.g., something like the following:

Matrix4f m;
auto it = m.line_cbegin();
while(it != m.line_cend()) {
  ...
  some_operation(*it)  //*it is expected to be a Vector4d object
  ...
  it++;
}

So is there any such iterator available in Eigen? I have checked some of Eigen documentation but did not find any, so I ask here in case I missed it. Thank you.


Solution

  • You need to get the head of Eigen's devel branch, then simply do as the doc says:

    for(auto row : m.rowwise())
      some_operation(row);
    

    Of course this example means that you can also call begin()/end() or cbegin()/cend() on m.rowwise().