将特征矩阵转换为 C 数组 [英] Convert Eigen Matrix to C array

查看:69
本文介绍了将特征矩阵转换为 C 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eigen 库可以将现有内存映射到 Eigen 矩阵.

The Eigen library can map existing memory into Eigen matrices.

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数组一样存储?

My question is, how can we get c array (e.g. float[] a) from eigen matrix (e.g. Matrix3f m)? What it the real layout of eigen matrix? Is the real data stored as in normal c array?

推荐答案

您可以使用 data() 特征矩阵类的成员函数.默认布局是列优先,而不是行优先作为多维 C 数组(可以在创建 Matrix 对象时选择布局).对于稀疏矩阵,前面的句子显然不适用.

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.

示例:

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.

这篇关于将特征矩阵转换为 C 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆