映射一个特征矩阵到一个数组c [英] Map a Eigen Matrix to an C array
问题描述
我最近开始使用征库。我得到了一个特征矩阵映射到C / C ++数组的一个问题。一个特征矩阵是默认主修列。所以,如果我用下面的code到A矩阵映射到C / C ++阵列,
双A [10];
双* P =&放大器;一个[0];
MatrixXd(2,5)米;
的for(int i = 0;我2;我++)
为(中间体J = 0; J&小于5; J ++)
米(I,J)= I + J;
COUT<< M<< ENDL;
本征::地图< MatrixXd>(P,2,5)= M;
的for(int i = 0;我小于10;我++)
COUT&所述;&下;一[1] - ;&下;;
COUT<< ENDL;
输出是:
0 1 2 3 4
1 2 3 4 5
0 1 1 2 2 3 3 4 4 5
如果我改变m的定义为连续主修:
矩阵和LT;双,2,5,RowMajor>米;
我所期望的输出是这样的:
0 1 2 3 4
1 2 3 4 5
0 1 2 3 4 1 2 3 4 5
但实际上,结果仍是相同的第一个。我的问题是,有没有这样的阵列中的数据是基于行映射的特征矩阵到C / C ++数组的方式?
我发现我可以使用matrix.data()memember函数来获得期望的结果,但我不知道我是否能做到这一点利用图:
使用matrix.data()的作品:
双A [10];
双* P =&放大器;一个[0];
矩阵LT;双,2,5,RowMajor>米;
的for(int i = 0;我2;我++)
为(中间体J = 0; J&小于5; J ++)
米(I,J)= I + J;
双* P1 = m.data();
的for(int i = 0;我小于10;我++)
COUT&所述;&下; P1 [1] - ;&下;;
COUT<< ENDL;
这不是矩阵类型 M
的事项,但在<$ C使用的类型$ C>地图模板。你必须改变在地图中使用
模板被排主要的类型。
征::地图&LT;矩阵和LT;双,2,5,RowMajor&GT; &GT;(P,2,5)=米;
I recently started to use the Eigen library. I got a question of mapping an Eigen matrix to a C/C++ array. An Eigen matrix is column majored by default. So if i use the following code to map a matrix to an C/C++ array,
double a[10];
double *p = &a[0];
MatrixXd(2,5) m;
for (int i=0; i<2;i++)
for (int j=0; j<5;j++)
m(i,j) = i+j;
cout<<m<<endl;
Eigen::Map<MatrixXd>(p,2,5) = m;
for (int i=0; i<10; i++)
cout<<a[i]<<" ";
cout<<endl;
The output is:
0 1 2 3 4
1 2 3 4 5
0 1 1 2 2 3 3 4 4 5
If I change the the definition of m as row majored:
Matrix <double,2,5,RowMajor> m;
i expected the output looks like this:
0 1 2 3 4
1 2 3 4 5
0 1 2 3 4 1 2 3 4 5
But actually the result was still the same as the first one. My question is that is there a way to map an Eigen matrix to an C/C++ array so that the data of the array is row based?
I found that I can use the matrix.data() memember function to get the desired result, but I'm wondering whether I can do this use map:
Use matrix.data() works:
double a[10];
double *p = &a[0];
Matrix <double,2,5,RowMajor> m;
for (int i=0; i<2;i++)
for (int j=0; j<5;j++)
m(i,j) = i+j;
double *p1 = m.data();
for (int i=0; i<10; i++)
cout<<p1[i]<<" ";
cout<<endl;
It's not the type of the matrix m
that matters, but the type used in the Map
template. You have to change the type used in the Map
template to be row major.
Eigen::Map<Matrix<double,2,5,RowMajor> >(p,2,5) = m;
这篇关于映射一个特征矩阵到一个数组c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!