Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 以CSV格式写入特征向量xd_C++_Eigen_Eigen3 - Fatal编程技术网

C++ 以CSV格式写入特征向量xd

C++ 以CSV格式写入特征向量xd,c++,eigen,eigen3,C++,Eigen,Eigen3,我正在尝试将一个Eigen::VectorXd写入CSV文件。向量来自一行特征::矩阵XXD。我的职能定义如下: void writeMatrixToCSVwithID(fs::path path, VectorXd row, unsigned long int row_id){ const static IOFormat CSVFormat(StreamPrecision, DontAlignCols, ", ", "\n"); ofstream file(path.c_str

我正在尝试将一个Eigen::VectorXd写入CSV文件。向量来自一行特征::矩阵XXD。我的职能定义如下:

void writeMatrixToCSVwithID(fs::path path, VectorXd row, unsigned long int row_id){
    const static IOFormat CSVFormat(StreamPrecision, DontAlignCols, ", ", "\n");
    ofstream file(path.c_str(), std::ofstream::out | std::ofstream::app);
    row.resize(1, row.size());
    file << row_id << ", " << row.format(CSVFormat) << std::endl;
    file.close();
}
预期产出为:

11, 0.247795, 0.327012, 0.502336, 0.569316, 0.705254
12, 0.247795, 0.327012, 0.502336, 0.569316, 0.705254

我需要更改什么?

错误的原因是Eigen将VectorXd作为列输出。rowid返回块,该块似乎将行或列提取作为列输出

因此,我不再传递VectorXd行,而是将该行作为MatrixXd传递。IOFormat对象是用行分隔符“,”初始化的

void writeMatrixToCSVwithID(fs::path path, MatrixXd row, unsigned long int row_id){
    const static IOFormat CSVFormat(StreamPrecision, DontAlignCols, ", ", ", ");
    ofstream file(path.c_str(), std::ofstream::app);
    row.resize(1, row.size()); // Making sure that we are dealing with a row.
    file << row_id << ", " << row.format(CSVFormat) << std::endl;
    file.close();
}
这将生成所需的行输出

void writeMatrixToCSVwithID(fs::path path, MatrixXd row, unsigned long int row_id){
    const static IOFormat CSVFormat(StreamPrecision, DontAlignCols, ", ", ", ");
    ofstream file(path.c_str(), std::ofstream::app);
    row.resize(1, row.size()); // Making sure that we are dealing with a row.
    file << row_id << ", " << row.format(CSVFormat) << std::endl;
    file.close();
}