Eigen::Matrix<type, row, col> 默认是行优先,因此在做数据传递时要特别谨慎. 特别是,c++中的数据传递到Eigen中时,要指定行优先,才不至于出问题:Eigen::Map<Eigen::Matrix<float, 3, 2, Eigen::RowMajor>> col_x2(arr)
另外,Eigen矩阵中,行优先和列优先的矩阵相互转换时,只是将数据的储存方式进行的转变,但是输出某个行列上的数据是不会变化的.例如下列代码的demo4
示例:
#include <iostream>
// Eigen
#include <Eigen/Core>
#include <Eigen/Geometry>
using namespace std;
int main()
{
// demo1
Eigen::Matrix<float,3,2> col_x;
col_x << 1, 2, 3, 4, 5, 6;
std::cout << "col major 1:\n" << col_x << std::endl;
// demo2
float arr[6] = {1,2,3,4,5,6};
Eigen::Map<Eigen::Matrix<float,3,2>> col_x2(arr);
std::cout << "col major 2:\n" << col_x2 << std::endl;
// demo3
Eigen::Matrix<float,3,2,Eigen::RowMajor> row_x;
row_x << 1, 2, 3,4,5,6;
std::cout << "row major 1:\n" << row_x << std::endl;
// demo4
Eigen::Matrix<float,3,2,Eigen::RowMajor> row_x2;
row_x2 = col_x;
std::cout << "row major 2:\n" << row_x2 << std::endl;
return true;
}
输出为:
col major 1:
1 2
3 4
5 6
col major 2:
1 4
2 5
3 6
row major 1:
1 2
3 4
5 6
row major 2:
1 2
3 4
5 6