Convert Eigen Matrix to C array

后端 未结 7 1924
深忆病人
深忆病人 2020-11-30 03:33

The Eigen library can map existing memory into Eigen matrices.

float array[3];
Map(array, 3).fill(10);
int data[4] = 1, 2, 3, 4;
Matrix2i mat         


        
7条回答
  •  [愿得一人]
    2020-11-30 04:17

    To convert normal data type to eigen matrix type

      double *X; // non-NULL pointer to some data
    

    You can create an nRows x nCols size double matrix using the Map functionality like this:

      MatrixXd eigenX = Map( X, nRows, nCols );
    

    To convert eigen matrix type into normal data type

      MatrixXd resultEigen;   // Eigen matrix with some result (non NULL!)
      double *resultC;        // NULL pointer <-- WRONG INFO from the site. resultC must be preallocated!
      Map( resultC, resultEigen.rows(), resultEigen.cols() ) =   resultEigen;
    

    In this way you can get in and out from eigen matrix. Full credits goes to http://dovgalecs.com/blog/eigen-how-to-get-in-and-out-data-from-eigen-matrix/

提交回复
热议问题