C++ 通过cvs格式文件构建Eigen的Matrix对象

来源:互联网 发布:郭艾伦奥运会数据 编辑:程序博客网 时间:2024/06/18 05:58

C++ 通过cvs格式文件构建Eigen的Matrix对象

flyfish

读取文件,写入vector,然后构建

调试环境VC++2017

template<typename T>T load_csv1(const std::string & path) {    std::ifstream in;    in.open(path);    std::string line;    std::vector<double> values;    UINT rows = 0;    while (std::getline(in, line))    {        std::stringstream ss(line);        std::string cell;        while (std::getline(ss, cell, ','))         {            double val = std::stod(cell);            values.push_back(val);        }        ++rows;    }    return Eigen::Map<const Eigen::Matrix<        typename T::Scalar,         T::RowsAtCompileTime,         T::ColsAtCompileTime,        RowMajor>>(values.data(), rows, values.size() / rows);}

Eigen::Map类 是 Eigen中将 “raw” C/C++ arrays 映射为 矩阵的类

Eigen::Map类 说明链接

使用方法

MatrixXd A = load_csv<MatrixXd>("C:\\test.csv");std::cout << "Left : " << A.leftCols(1) << std::endl;std::cout << "Right : " << A.rightCols(1) << std::endl;
原创粉丝点击