Search code examples
matrixconstructoreigen3

Eigen3 : dynamic matrix along one dimension only : failure to contruct


I can easily define a matrix type of fixed lines number and undefined columns number :

  typedef Matrix<double, 6, Dynamic> dMat6rowsNCols;
  dMat6rowsNCols M1;

But I couldn't figure out how to instanciate it, those attempts don't compile :

  M1.Zero(4);
  M1 = dMat6rowsNCols::Zero(4);

May I ask you some hints ?

Cheers

Sylvain


Solution

  • From the documentation of DenseBase::Zero(Index):

    This is only for vectors (either row-vectors or column-vectors), i.e. matrices which are known at compile-time to have either one row or one column.

    You have to use DenseBase::Zero(Index, Index):

    M1 = dMat6rowsNCols::Zero(6, 4);
    

    If you are using the current Eigen trunk you can use Eigen::NoChange for some functions, e.g.

    M1.setZero(Eigen::NoChange, 4);
    

    As a side note: Calling M1.Zero does not call the member function that sets all coefficients to 0 but is a less common way of calling the static function DenseBase::::Zero. You are probably looking for M1.setZero.