typecasting Eigen::VectorXd to std::vector

前端 未结 3 1892
滥情空心
滥情空心 2020-12-13 13:37

Their are many links to go the other way round but I am unable to find to get a std::vector from a Eigen::Matrix or Eigen::VectorXd in my specific case.

相关标签:
3条回答
  • 2020-12-13 14:22

    You can do this from and to Eigen vector :

        //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;
        }
    
    0 讨论(0)
  • 2020-12-13 14:25
    vector<int> vec(mat.data(), mat.data() + mat.rows() * mat.cols());
    
    0 讨论(0)
  • 2020-12-13 14:30

    You cannot typecast, but you can easily copy the data:

    VectorXd v1;
    v1 = ...;
    vector<double> v2;
    v2.resize(v1.size());
    VectorXd::Map(&v2[0], v1.size()) = v1;
    
    0 讨论(0)
提交回复
热议问题