本文共 2379 字,大约阅读时间需要 7 分钟。
注意,必须使用C++11以上的编译器进行编译
g++ -g -Wall -std=c++11 test.cpp -o test
#include#include "Eigen/Core"#include using namespace std;using namespace Eigen;int main(){ MatrixXd test = MatrixXd::Random(2,3); cout << test < (array, 3, 2); cout << "---\n" < << endl; MatrixXd test2 = test1.transpose(); cout << "---\n" << test2 < test3 = Map (array, 2, 3); cout << "---\n" << test3 < >(array); cout << "---\n"<< test4 < > test5(array,2,3); cout << "---\n" << test5 <
运行结果如下:
» .\test.exe -0.997497 -0.613392 0.170019 0.127171 0.617481 -0.0402539-0.9974970.127171-0.6133920.6174810.170019-0.0402539---1 42 53 6---1 2 34 5 6---1 3 52 4 6---1 2 34 5 6---1 2 34 5 6
// eigMat1和array指向的是同一个内存空间,是绑定在一起的MapeigMat1(array, 3, 3); // eigMat1和array指向不同的内存空间,互不影响 MatrixXi eigMat2 = Map (array, 3, 3);
测试代码:
#include#include "Eigen/Core"using namespace std;using namespace Eigen;int main(){ int array[9]={ 1,2,3,4,5,6,7,8,9}; Map eigMat1(array, 3, 3); // eigMat1和array指向的是同一个内存空间,是绑定在一起的 cout<<"----"< (array, 3, 3); // eigMat1和array指向不同的内存空间,互不影响 cout<<"----"<
int array[9];cout << "colMajor matrix = \n" << Map(array) << endl; // map a contiguous array as a column-major matrix// 必须使用c++11以上的编译器编译,否则报错cout << "rowMajor matrix = \n" << Map >(array) << endl; // map a contiguous array as a row-major matrix
g++ -g -Wall -std=c++11 test.cpp -o test
Matrix3d eigMat;// 法1double* eigMatptr = eigMat.data();// 法2double* eigMatptrnew = new double[eigMat.size()];Map(eigMatptrnew, eigMat.rows(), eigMat.cols()) = eigMat;
测试代码如下:
#include#include "Eigen/Core"using namespace std;using namespace Eigen;int main(){ Matrix3d eigMat; eigMat << 1,2,3, 4,5,6, 7,8,9; double* eigMatptr = eigMat.data(); double* eigMatptrnew = new double[eigMat.size()]; Map (eigMatptrnew, eigMat.rows(), eigMat.cols()) = eigMat; for(int i=0;i<9;i++) { cout<<"---"< (eigMatptrnew, eigMat.rows(), eigMat.cols())<
参考链接:
转载地址:http://wyzxf.baihongyu.com/