【发布时间】:2015-05-12 04:42:00
【问题描述】:
Eigen 是 C++ 中的线性代数库。我在 std::vector (下面代码中的 DataVector )类型数组中有我的数据(双精度类型)。我尝试使用以下代码逐行复制它,该代码仍然按列给出结果。
Map<MatrixXd, RowMajor> MyMatrix(DataVector.data(), M, N);
我在这里的语法是否正确?
【问题讨论】:
Eigen 是 C++ 中的线性代数库。我在 std::vector (下面代码中的 DataVector )类型数组中有我的数据(双精度类型)。我尝试使用以下代码逐行复制它,该代码仍然按列给出结果。
Map<MatrixXd, RowMajor> MyMatrix(DataVector.data(), M, N);
我在这里的语法是否正确?
【问题讨论】:
没有。 MatrixXd 对象必须定义为行/列专业。请参阅下面的示例。
#include <Eigen/Core>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
int main(int argc, char *argv[])
{
std::vector<int> dat(4);
int i = 0;
dat[i] = i + 1; i++;
dat[i] = i + 1; i++;
dat[i] = i + 1; i++;
dat[i] = i + 1;
typedef Eigen::Matrix<int, -1, -1, Eigen::ColMajor> Cm;
Eigen::Map<Cm> m1(dat.data(), 2, 2);
cout << m1 << endl << endl;
typedef Eigen::Matrix<int, -1, -1, Eigen::RowMajor> Rm;
Eigen::Map<Rm> m2(dat.data(), 2, 2);
cout << m2 << endl << endl;
return 0;
}
输出:
1 3
2 4
1 2
3 4
【讨论】: