gpt4 book ai didi

c++ - 将矩阵写入 Eigen 文件?

转载 作者:IT老高 更新时间:2023-10-28 22:59:30 27 4
gpt4 key购买 nike

我正在尝试使用 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;
}

如何将 m 导出到文本文件(我已搜索文档并且没有发现提到写作功能)?

最佳答案

如果你可以在 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';
}
}

关于c++ - 将矩阵写入 Eigen 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9496867/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com