How to read CSV file and assign to Eigen Matrix?

后端 未结 3 1297
再見小時候
再見小時候 2020-12-19 15:27

I try to read a large cvs file into Eigen Matrix, below the code found having problem where it can not detect each line of \\n in cvs file to create multiple rows in the mat

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 15:54

    Read the CSV file into your vector < vector > as you please (e.g. Lucas's answer). Instead of the vector< vector > construct, use a vector< vector > or even better a simple vector< double >. To assign the vector of vectors to an Eigen matrix efficiently using vector< vector< double > >, use the following:

    Eigen::MatrixXcd mat(rows, cols);
    for(int i = 0; i < rows; i++)
        mat.row(i) = Eigen::Map (csvData[i].data(), cols).cast >();
    

    If you opted to use the vector< double > option, it becomes:

    Eigen::MatrixXcd mat(rows, cols);
    mat = Eigen::Map (csvData.data(), rows, cols).cast >().transpose();
    

提交回复
热议问题