在特征中写矩阵到文件?

use*_*035 13 c++ eigen

我正在尝试使用Eigen库学习C++.

int main(){
    MatrixXf m = MatrixXf::Random(30,3);
    cout << "Here is the matrix m:\n" << m << endl;
    cout << "m" << endl <<  colm(m) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如何导出m到文本文件(我搜索过文档但没有找到写入功能)?

coo*_*451 15

如果你可以在cout上编写它,它适用于任何std :: ostream:

#include <fstream>

int main()
{
  std::ofstream file("test.txt");
  if (file.is_open())
  {
    MatrixXf m = MatrixXf::Random(30,3);
    file << "Here is the matrix m:\n" << m << '\n';
    file << "m" << '\n' <<  colm(m) << '\n';
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 什么是colm()应该做什么?它对我不起作用. (7认同)