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指向的是同一个内存空间,是绑定在一起的Map
eigMat1(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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:动力学方程MCG矩阵的计算
下一篇:Matlab中legend的位置

发表评论

最新留言

很好
[***.229.124.182]2024年04月30日 18时50分44秒