Convert Eigen Matrix to C array

后端 未结 7 1941
深忆病人
深忆病人 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:04

    You can use the data() member function of the Eigen Matrix class. The layout by default is column-major, not row-major as a multidimensional C array (the layout can be chosen when creating a Matrix object). For sparse matrices the preceding sentence obviously doesn't apply.

    Example:

    ArrayXf v = ArrayXf::LinSpaced(11, 0.f, 10.f);
    // vc is the corresponding C array. Here's how you can use it yourself:
    float *vc = v.data();
    cout << vc[3] << endl;  // 3.0
    // Or you can give it to some C api call that takes a C array:
    some_c_api_call(vc, v.size());
    // Be careful not to use this pointer after v goes out of scope! If
    // you still need the data after this point, you must copy vc. This can
    // be done using in the usual C manner, or with Eigen's Map<> class.
    

提交回复
热议问题