类型化Eigen :: VectorXd到std :: vector

时间:2014-09-29 07:14:35

标签: c++ eigen

他们有很多链接可以反过来但我无法在特定情况下找到从Eigen :: Matrix或Eigen :: VectorXd获取std :: vector。

3 个答案:

答案 0 :(得分:38)

您无法进行类型转换,但可以轻松复制数据:

VectorXd v1;
v1 = ...;
vector<double> v2;
v2.resize(v1.size());
VectorXd::Map(&v2[0], v1.size()) = v1;

答案 1 :(得分:25)

vector<int> vec(mat.data(), mat.data() + mat.rows() * mat.cols());

答案 2 :(得分:1)

您可以从Eigen向执行此操作:

    //init a first vector
    std::vector<float> v1;
    v1.push_back(0.5);
    v1.push_back(1.5);
    v1.push_back(2.5);
    v1.push_back(3.5);

    //from v1 to an eignen vector
    float* ptr_data = &v1[0];
    Eigen::VectorXf v2 = Eigen::Map<Eigen::VectorXf, Eigen::Unaligned>(v1.data(), v1.size());

    //from the eigen vector to the std vector
    std::vector<float> v3(&v2[0], v2.data()+v2.cols()*v2.rows());


    //to check
    for(int i = 0; i < v1.size() ; i++){
        std::cout << std::to_string(v1[i]) << " | " << std::to_string(v2[i]) << " | " << std::to_string(v3[i]) << std::endl;
    }