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
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();