How to get the number of rows and columns of an Eigen::MatrixXd?

Tom Oconnor picture Tom Oconnor · Aug 1, 2018 · Viewed 8.2k times · Source

I am trying to traverse Eigen::MatrixXd matrix. However, there does not seem to be a function that returns the columns size nor the row size. Does anybody have an idea on how to do this?

Answer

Icarus3 picture Icarus3 · Aug 1, 2018

This should work...

#include <Eigen/Dense>

int main()
{
    Eigen::MatrixXd matrix(3, 4);

    int r = matrix.rows();
    int c = matrix.cols();

    for (int i = 0; i < r; ++i)
    {
        for (int j = 0; j < c; ++j)
        {
            std::cout << matrix(i,j);
        }
    }

    return 0;
}