Eigen3矩阵与数组的互相转换
发布日期:2021-07-01 04:03:38
浏览次数:3
分类:技术文章
本文共 2379 字,大约阅读时间需要 7 分钟。
文章目录
1. Demo
注意,必须使用C++11以上的编译器进行编译
g++ -g -Wall -std=c++11 test.cpp -o test
#include#include "Eigen/Core"#include using namespace std;using namespace Eigen;int main(){ MatrixXd test = MatrixXd::Random(2,3); cout << test < (array, 3, 2); cout << "---\n" < << endl; MatrixXd test2 = test1.transpose(); cout << "---\n" << test2 < test3 = Map (array, 2, 3); cout << "---\n" << test3 < >(array); cout << "---\n"<< test4 < > test5(array,2,3); cout << "---\n" << test5 <
运行结果如下:
» .\test.exe -0.997497 -0.613392 0.170019 0.127171 0.617481 -0.0402539-0.9974970.127171-0.6133920.6174810.170019-0.0402539---1 42 53 6---1 2 34 5 6---1 3 52 4 6---1 2 34 5 6---1 2 34 5 6
2. 数组转化为Eigen::Matrix两种绑定方法的区别
// eigMat1和array指向的是同一个内存空间,是绑定在一起的MapeigMat1(array, 3, 3); // eigMat1和array指向不同的内存空间,互不影响 MatrixXi eigMat2 = Map (array, 3, 3);
测试代码:
#include#include "Eigen/Core"using namespace std;using namespace Eigen;int main(){ int array[9]={ 1,2,3,4,5,6,7,8,9}; Map eigMat1(array, 3, 3); // eigMat1和array指向的是同一个内存空间,是绑定在一起的 cout<<"----"< (array, 3, 3); // eigMat1和array指向不同的内存空间,互不影响 cout<<"----"<
3. 数组转换为Eigen::Matrix
int array[9];cout << "colMajor matrix = \n" << Map(array) << endl; // map a contiguous array as a column-major matrix// 必须使用c++11以上的编译器编译,否则报错cout << "rowMajor matrix = \n" << Map >(array) << endl; // map a contiguous array as a row-major matrix
g++ -g -Wall -std=c++11 test.cpp -o test
4. Eigen::Matrix转化为数组的两种方法
Matrix3d eigMat;// 法1double* eigMatptr = eigMat.data();// 法2double* eigMatptrnew = new double[eigMat.size()];Map(eigMatptrnew, eigMat.rows(), eigMat.cols()) = eigMat;
测试代码如下:
#include#include "Eigen/Core"using namespace std;using namespace Eigen;int main(){ Matrix3d eigMat; eigMat << 1,2,3, 4,5,6, 7,8,9; double* eigMatptr = eigMat.data(); double* eigMatptrnew = new double[eigMat.size()]; Map (eigMatptrnew, eigMat.rows(), eigMat.cols()) = eigMat; for(int i=0;i<9;i++) { cout<<"---"< (eigMatptrnew, eigMat.rows(), eigMat.cols())<
参考链接:
转载地址:https://miracle.blog.csdn.net/article/details/116382073 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月30日 18时50分44秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
[转帖]Robots.txt指南
2019-05-01
Eclipse + MyEclipse下配置J2EE工程(英文界面)
2019-05-01
Eclipse及其插件下载网址大全
2019-05-01
正则表达式简介(微软)--6.优先权顺序
2019-05-01
多用户与多租户的区别
2019-05-01
Python自动化运维 - day14 - JavaScript基础
2019-05-02
oracle保存小数点前为"0"的问题
2019-05-02
linux sar 命令详解
2019-05-02
ipvsadm 安装配置
2019-05-02
Linux shell脚本的字符串截取
2019-05-02
数据库复习(4)
2019-05-02
1小时点击量破千万!阿里巴巴首发:MySQL高级调优笔记!全是技术重点
2019-05-02
这个GItHub上的Java项目开源了 2021最全的Java架构面试复习指南
2019-05-02
Proftpd MySQL [Step by Step]
2019-05-02
EFI Shell 命令参考
2019-05-02
HP-UX oracle RAC 双机实践
2019-05-02
解决SHELL脚本中的export无法生效的问题【转】
2019-05-02