1. 数组转化为Eigen::Matrix

int array[];

cout << "colMajor matrix = \n" << Map<Matrix3i>(array) << endl;                      // map a contiguous array as a column-major matrix
cout << "rowMajor matrix = \n" << Map<Matrix<int, , , RowMajor>>(array) << endl; // map a contiguous array as a row-major matrix Map<MatrixXi> eigMat1(array, , ); // eigMat1和array指向的是同一个内存空间,是绑定在一起的
MatrixXi eigMat2 = Map<MatrixXi>(array, , ); // eigMat1和array指向不同的内存空间,互不影响

2. Eigen::Matrix转化为数组

Matrix3d eigMat;

double* eigMatptr = eigMat.data();
double* eigMatptrnew = new double[eigMat.size()];
Map<MatrixXd>(eigMatptrnew, eigMat.rows(), eigMat.cols()) = eigMat;

3.更多转化

下面的代码是我写的互相转化的测试

#include <iostream>
#include <vector>
#include <Eigen/Dense> using namespace std;
using namespace Eigen; void array2eigenMat();
void eigenMat2array(); void array2eigenVec();
void eigenVec2array(); void vector2eigenMat();
void eigenMat2vector(); void vector2eigenVec();
void eigenVec2vector(); int main()
{
cout << "hello world" <<endl;
array2eigenMat();
eigenMat2array(); array2eigenVec();
eigenVec2array(); vector2eigenMat();
eigenMat2vector(); vector2eigenVec();
eigenVec2vector();
return ;
} void array2eigenMat()
{
cout << "-------------------------- array2eigenMat --------------------------" << endl; int array[];
for (int i = ; i < ; ++i) array[i] = i;
cout << "array = [ "; for (int i = ; i < ; ++i) cout << array[i] << " "; cout << "]" << endl; cout << "colMajor matrix = \n" << Map<Matrix3i>(array) << endl; // map a contiguous array as a column-major matrix
cout << "rowMajor matrix = \n" << Map<Matrix<int, , , RowMajor>>(array) << endl; // map a contiguous array as a row-major matrix cout << "stride matrix = \n" << Map<MatrixXi, , OuterStride<>>(array, , , OuterStride<>()) << endl;
//mapping an array while specifying an outer stride. Here, since we're mapping as a column-major matrix,
// 'outer stride' means the pointer increment between two consecutive columns Map<MatrixXi> eigMat1(array, , );
MatrixXi eigMat2 = Map<MatrixXi>(array, , );
array[] = ; cout << "eigMat1 matrix = \n"; cout << eigMat1 << endl;
cout << "eigMat2 matrix = \n"; cout << eigMat2 << endl;
cout << "---------------------------------------------------------------------" << endl; }
void eigenMat2array()
{
cout << "-------------------------- eigenMat2array --------------------------" << endl;
Matrix3d eigMat;
eigMat <<
, , ,
, , ,
, , ;
cout << "init eigMat = \n"; cout << eigMat << endl; double* eigMatptr = eigMat.data();
cout << "array = [ "; for (int i = ; i < ; ++i) cout << eigMatptr[i] << " "; cout << "]" << endl; eigMat(, ) = ;
cout << "array = [ "; for (int i = ; i < ; ++i) cout << eigMatptr[i] << " "; cout << "]" << endl; double *eigMatptrnew = new double[eigMat.size()];
Map<MatrixXd>(eigMatptrnew, eigMat.rows(), eigMat.cols()) = eigMat; eigMat(, ) = ;
cout << "init matrix = \n"; cout << eigMat << endl;
cout << "array = [ "; for (int i = ; i < ; ++i) cout << eigMatptr[i] << " "; cout << "]" << endl;
cout << "---------------------------------------------------------------------" << endl;
} void array2eigenVec()
{
cout << "-------------------------- array2eigenVec --------------------------" << endl; int array[];
for (int i = ; i < ; ++i) array[i] = i;
cout << "data array = [ "; for (int i = ; i < ; ++i) cout << array[i] << " "; cout << "]" << endl; Map<VectorXi> eigVec(array, );
cout << "eigen vector transpose = " << eigVec.transpose() << endl;
cout << "stride vector transpose = " << Map<VectorXi, , InnerStride<> >(array, ).transpose() << endl;
// map an array as a vector, specifying an inner stride, that is, the pointer increment between two consecutive coefficients array[] = ;
cout << "eigen vector transpose = " << eigVec.transpose() << endl;
cout << "stride vector transpose = " << Map<VectorXi, , InnerStride<> >(array, ).transpose() << endl; cout << "---------------------------------------------------------------------" << endl;
}
void eigenVec2array()
{
cout << "-------------------------- eigenVec2array --------------------------" << endl;
VectorXf eigvec();
eigvec << , , , , ;
cout << "eigen vector transpose = " << eigvec.transpose() << endl; float *array = new float;
array = eigvec.data();
cout << "data array = [ "; for (int i = ; i < eigvec.size(); ++i) cout << array[i] << " "; cout << "]" << endl; eigvec() = ;
cout << "data array = [ "; for (int i = ; i < eigvec.size(); ++i) cout << array[i] << " "; cout << "]" << endl; array[] = ;
cout << "eigen vector transpose = " << eigvec.transpose() << endl; cout << "---------------------------------------------------------------------" << endl;
} void vector2eigenMat()
{
cout << "-------------------------- vector2eigenMat --------------------------" << endl;
vector<int> stdvec{ , , , , , , , , };
Map<Matrix<int, , , RowMajor>> eigMat1(stdvec.data());
MatrixXi eigMat2 = Map<Matrix<int, , , RowMajor>>(stdvec.data()); cout << "eigMat1 matrix = \n"; cout << eigMat1 << endl;
cout << "eigMat2 matrix = \n"; cout << eigMat2 << endl; stdvec[] = ;
cout << "eigMat1 matrix = \n"; cout << eigMat1 << endl;
cout << "eigMat2 matrix = \n"; cout << eigMat2 << endl; cout << "---------------------------------------------------------------------" << endl;
}
void eigenMat2vector()
{
cout << "-------------------------- eigenMat2vector --------------------------" << endl;
Matrix3d eigMatCol;
eigMatCol <<
, , ,
, , ,
, , ;
cout << "eigen matrix col = \n"; cout << eigMatCol << endl;
vector<double> stdvec1(eigMatCol.data(), eigMatCol.data() + eigMatCol.size());
cout << "std vector1 = ["; for (int i = ; i < stdvec1.size(); ++i) cout << stdvec1[i] << " "; cout << "]" << endl; Matrix<double, , , RowMajor> eigMatRow = eigMatCol;
cout << "eigen matrix row = \n"; cout << eigMatCol << endl;
vector<double> stdvec2(eigMatRow.data(), eigMatRow.data() + eigMatRow.size());
cout << "std vector2 = ["; for (int i = ; i < stdvec2.size(); ++i) cout << stdvec2[i] << " "; cout << "]" << endl; cout << "---------------------------------------------------------------------" << endl;
} void vector2eigenVec()
{
cout << "-------------------------- vector2eigenVec --------------------------" << endl;
vector<int> stdvec{ , , , , };
cout << "std vector = ["; for (int i = ; i < stdvec.size(); ++i) cout << stdvec[i] << " "; cout << "]" << endl; Map<VectorXi> eigVec1(stdvec.data(), stdvec.size());
VectorXi eigVec2 = Map<VectorXi>(stdvec.data(), stdvec.size());
cout << "eigen vector1 transpose = " << eigVec1.transpose() << endl;
cout << "eigen vector2 transpose = " << eigVec2.transpose() << endl;
cout << "stride vector transpose = " << Map<VectorXi, , InnerStride<> >(stdvec.data(), ).transpose() << endl; stdvec[] = ;
cout << "eigen vector1 transpose = " << eigVec1.transpose() << endl;
cout << "eigen vector2 transpose = " << eigVec2.transpose() << endl; cout << "stride vector transpose = " << Map<VectorXi, , InnerStride<> >(stdvec.data(), ).transpose() << endl; cout << "---------------------------------------------------------------------" << endl;
}
void eigenVec2vector()
{
cout << "-------------------------- eigenVec2vector --------------------------" << endl;
VectorXf eigvec();
eigvec << , , , , ;
cout << "eigen vector transpose = " << eigvec.transpose() << endl; vector<float> stdvec(eigvec.data(), eigvec.data() + eigvec.size());
cout << "std vector = ["; for (int i = ; i < stdvec.size(); ++i) cout << stdvec[i] << " "; cout << "]" << endl; eigvec() = ;
cout << "std vector = ["; for (int i = ; i < stdvec.size(); ++i) cout << stdvec[i] << " "; cout << "]" << endl;
cout << "---------------------------------------------------------------------" << endl;
}

程序运行结果

Eigen::Matrix与array数据转换-LMLPHP

4. 参考

1. Eigen::Map

2. Eigen quick reference guide

3. Vlad's Blog

05-11 19:29