How to read CSV file and assign to Eigen Matrix?

后端 未结 3 1296
再見小時候
再見小時候 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<string> > construct, use a vector< vector<double> > 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<Eigen::VectorXd> (csvData[i].data(), cols).cast<complex<double> >();
    

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

    Eigen::MatrixXcd mat(rows, cols);
    mat = Eigen::Map<Eigen::VectorXd> (csvData.data(), rows, cols).cast<complex<double> >().transpose();
    
    0 讨论(0)
  • 2020-12-19 15:59

    Here's something you can actually copy-paste

    Writing your own "parser"

    Pros: lightweight and customizable

    Cons: customizable

    #include <Eigen/Dense>
    #include <vector>
    #include <fstream>
    
    using namespace Eigen;
    
    template<typename M>
    M load_csv (const std::string & path) {
        std::ifstream indata;
        indata.open(path);
        std::string line;
        std::vector<double> values;
        uint rows = 0;
        while (std::getline(indata, line)) {
            std::stringstream lineStream(line);
            std::string cell;
            while (std::getline(lineStream, cell, ',')) {
                values.push_back(std::stod(cell));
            }
            ++rows;
        }
        return Map<const Matrix<typename M::Scalar, M::RowsAtCompileTime, M::ColsAtCompileTime, RowMajor>>(values.data(), rows, values.size()/rows);
    }
    

    Usage:

    MatrixXd A = load_csv<MatrixXd>("C:/Users/.../A.csv");
    Matrix3d B = load_csv<Matrix3d>("C:/Users/.../B.csv");
    VectorXd v = load_csv<VectorXd>("C:/Users/.../v.csv");
    

    Using the armadillo library's parser

    Pros: supports other formats as well, not just csv

    Cons: extra dependency

    #include <armadillo>
    
    template <typename M>
    M load_csv_arma (const std::string & path) {
        arma::mat X;
        X.load(path, arma::csv_ascii);
        return Eigen::Map<const M>(X.memptr(), X.n_rows, X.n_cols);
    }
    
    0 讨论(0)
  • 2020-12-19 16:03

    This will read from a csv file correctly:

    std::ifstream indata;
    
    indata.open(filename);
    
    std::string                line;
    while (getline(indata, line))
    {
        std::stringstream          lineStream(line);
        std::string                cell;
    
        while (std::getline(lineStream, cell, ','))
        {
            //Process cell
        }
    }
    

    Edit: Also, since your csv is full of numbers, make sure to use std::stod or the equivalent conversion once you expect to treat them as such.

    0 讨论(0)
提交回复
热议问题