我的问题是如何初始化一个特征矩阵,但 不是 这样:
matrix << 1,0,1,0,
1,0,1,0,
1,0,1,0,
我有一个看起来像上面的矩阵(逗号或没有逗号无关紧要)存储在 txt 文件中。
我已经写了一个函数来读取每一行并将它放入一个向量现在我想用这个数据创建一个矩阵
但它不起作用,我找不到任何页面来解释如何在不写值的情况下将数据分配给矩阵。(如上面的示例)
我需要的只是特征矩阵中文件中的数据
到目前为止我尝试了什么:(PS:有迭代器的想法,但我想对于非常大的矩阵需要太长时间,我只是用 1-2 维矩阵尝试了这个例子)
int readFromFile (const char * path, vector <string> & mv)
{
fstream file;
string line;
file.open(path);
while (getline(file,line))
{
mv.push_back(line);
}
file.close();
return 0;
}
typedef Matrix <int, 1, 2> MyMatrix;
int fromVectoEigen (vector<string> & source, MyMatrix & target)
{ //for (int i = source.size(); i<0 ; i--)
//{
string valuerow = source.back();
string::iterator it = valuerow.begin();
target.row(0)<< *it;
target.row(0)<<*it+1;
//source.pop_back();
//}
return 0;
}
不幸的是,不能只说 Matrix.row(i) = vector.back()
这不起作用。
原文由 dieHellste 发布,翻译遵循 CC BY-SA 4.0 许可协议
以下代码适用于包含任意大小矩阵的文件:
问候。