将特征矩阵转换为 C 数组

2024-02-05

The Eigen http://eigen.tuxfamily.org/库可以将现有内存映射到特征矩阵。

float array[3];
Map<Vector3f>(array, 3).fill(10);
int data[4] = 1, 2, 3, 4;
Matrix2i mat2x2(data);
MatrixXi mat2x2 = Map<Matrix2i>(data);
MatrixXi mat2x2 = Map<MatrixXi>(data, 2, 2);

我的问题是,我们如何从特征矩阵(例如 Matrix3f m)中获取 c 数组(例如 float[] a)?特征矩阵的真实布局是什么?真实的数据是像普通的c数组一样存储的吗?


您可以使用data() http://eigen.tuxfamily.org/dox/classEigen_1_1PlainObjectBase.html#ac54123f62de4c46a9107ff53890b6116Eigen Matrix 类的成员函数。默认情况下,布局是列优先,而不是作为多维 C 数组的行优先(可以在创建 Matrix 对象时选择布局)。对于稀疏矩阵,前面的句子显然不适用。

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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将特征矩阵转换为 C 数组 的相关文章

随机推荐