Eigen library --> initialize matrix with data from file or existing std::vector content (c++)

前端 未结 7 1185
你的背包
你的背包 2020-12-25 08:00

My question is how to initialize an eigen Matrix, but NOT this way:

matrix << 1,0,1,0,
          1,0,1,0,
          1,0,1,0,
         


        
7条回答
  •  无人及你
    2020-12-25 08:41

    I used iterators to collect the data in a vector and then initialize the matrix. The conversion to the vector seem to be the time-consuming part of the method, which approximately has the same speed as the solutions above. Ideas on how to improve this would be interesting.

    template 
    using Tmat = Eigen::Matrix;
    
    Tmat txt_to_mat(std::string path, int rows, int cols)
    {
        std::ifstream fstr(path.c_str());
        std::vector data_vec = std::vector{
            std::istream_iterator(fstr),
            std::istream_iterator()
        };
    
        Tmat mat(rows, cols);
        for(int i=0; i

提交回复
热议问题