Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write matrix to file in eigen?

Tags:

c++

eigen

I am trying to learn C++ with the Eigen library.

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;
}

How can I export m to a text file (I have searched the documentations and have not found mention of an writing function)?

like image 563
user189035 Avatar asked Feb 29 '12 09:02

user189035


1 Answers

If you can write it on cout, it works for any 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';
  }
}
like image 55
cooky451 Avatar answered Sep 29 '22 04:09

cooky451