C++ 以CSV格式写入特征向量xd
我正在尝试将一个Eigen::VectorXd写入CSV文件。向量来自一行特征::矩阵XXD。我的职能定义如下: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
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();
}