C++ 数组直接存入已有的动态Eigen 矩阵 (Mapping array back to an existing Eigen matrix)
2018-03-02 17:45
597 查看
C++ 数组直接存入已有的动态Eigen 矩阵
Mapping array back to an existing Eigen matrix
准备工作:#include <Eigen/Dense> using namespace Eigen;
构建一个动态矩阵,并创建一个数据,一维即可:
MatrixXf testMat; float tt[] = {1.2, 2.2, 3.2, 4.2, 5.7, 6.1};
1.如果编译前已知将要创建的矩阵大小为2行3列,则:
testMat = Map<Matrix<float,2,3,RowMajor> >(tt);
结果如下 1.2 2.2 3.2 4.2 5.7 6.1
2.不完全利用矩阵所有数据也是可以的,如2行2列:
testMat = Map<Matrix<float,2,2,RowMajor> >(tt);
结果如下 1.2 2.2 3.2 4.2
3.如果在运行时才能得知要创建的矩阵大小为2行3列,则:
int rows =2; int cols =3; testMat = Map<Matrix<float,Dynamic,Dynamic,RowMajor> >(tt,rows,cols);
结果如下 1.2 2.2 3.2 4.2 5.7 6.1
4.如果testMat已经指向数据和类型,直接用memcpy对数据进行更改,则会发现矩阵默认是ColMajor的:
int rows =2; int cols =3; testMat = Map<Matrix<float,Dynamic,Dynamic,RowMajor> >(tt,rows,cols);
cout << testMat << endl;
float tt2[] = {7.1, 8.2, 9.3, 10.1, 11.2, 12.3};
memcpy(testMat.data(),tt2, sizeof(tt2));
cout << testMat << endl;
结果如下 1.2 2.2 3.2 4.2 5.7 6.1
7.1 9.3 11.2
8.2 10.1 12.3
5.从4中可以看出,
Map<Matrix<float,Dynamic,Dynamic,RowMajor> >(tt,rows,cols);应该类似一个生成器,其中的参数RowMajor和ColMajor决定了怎么去拷贝内存.如果直接按数组顺序来memcpy,则会得到和ColMajor一致的结果.而MatrixXf还是那个MatrixXf.
相关文章推荐
- 建空列表list,数组array,矩阵matrix
- Python Numpy的数组array和矩阵matrix
- string.ToCharArray() 装字符串的每个字符存入数组
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- javascript数组转换(convert to an Array)
- go []Type(数组) 无法直接转化为 []interface{}(can't I assign any slice to an []interface{})
- Python Numpy的数组array和矩阵matrix
- [Javascript] Different ways to create an new array/object based on existing array/object
- 【1.1】Eigen C++ 矩阵开源库学习之稠密矩阵和数组操作——矩阵类
- C++中要将字符串存入数组中时不能直接赋值
- C++初学之 1. 数组(菱形矩阵):动态建立数组,算法小结
- Python Numpy的数组array和矩阵matrix
- How to pass an array from C++ to an embedded python
- Python Numpy的数组array和矩阵matrix的用法与区别
- 数组指针:a pointer to an array,即指向数组的指针
- string.ToCharArray() 装字符串的每个字符存入数组
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)
- C++和Java动态数组比较
- C++创建动态数组
- C++ 指针 数组指针 二维数组与指针 直接解释底层 各种指针(字符指针,函数指针,数组指针)解释