c++ - 将数据从 std::vector 复制到 C++ 中的 Eigen 的 MatrixXd

标签 c++ eigen eigen3

Eigen 是 C++ 中的线性代数库。我的数据(double 类型)在 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

关于c++ - 将数据从 std::vector 复制到 C++ 中的 Eigen 的 MatrixXd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30181914/

相关文章:

c++ - 如何定义双括号/双迭代器运算符,类似于 Vector of Vectors'?

c++ - std::string 上的段错误

c++ - 如何读取 CSV 文件并分配给特征矩阵?

c++ - 正确使用 Eigen::Ref<> 类

c++ - Eigen 3.3.0 与 3.2.10 的性能回归?

c++ - 访问另一个类的数据,该类的对象作为构造函数中的参数发送

c++ - 75 :15: fatal error: stdlib. h : No such file or directory #include_next <stdlib. h>

c++ - 禁用 Eigen 表达式与 const 引用的临时绑定(bind)

c++ - 将特征向量复制到 C 数组?

c++ - Eigen C++;使用 Eigen::Transform 进行欧几里德变换