Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

eigen3

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?

like image 322
Tom Oconnor Avatar asked Aug 01 '18 19:08

Tom Oconnor


People also ask

Is Eigen row or column major?

The default in Eigen is column-major. Naturally, most of the development and testing of the Eigen library is thus done with column-major matrices. This means that, even though we aim to support column-major and row-major storage orders transparently, the Eigen library may well work best with column-major matrices.

What is VectorXd?

The next line of the main function introduces a new type: VectorXd . This represents a (column) vector of arbitrary size. Here, the vector v is created to contain 3 coefficients which are left uninitialized.

How do I resize an eigen matrix?

Resizing. The current size of a matrix can be retrieved by rows(), cols() and size(). These methods return the number of rows, the number of columns and the number of coefficients, respectively. Resizing a dynamic-size matrix is done by the resize() method.


1 Answers

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;
}
like image 139
Icarus3 Avatar answered Sep 23 '22 18:09

Icarus3