Eigen::Map使用说明

一个模板函数,用于将一串数据映射到一个矩阵或者向量中。

模板是

template<typename PlainObjectType, int MapOptions, typename StrideType>
class Eigen::Map< PlainObjectType, MapOptions, StrideType >

PlainObjectType:指定想要生成的矩阵或者向量的类型,比如Matrix3i,VectorXi,RowVectorXi

MapOptions:以字节为单位指定指针对齐方式,可以为Aligned8,Aligned16....

StrideType:指定“步幅”的种类,可以为外部或者内部,分别为OuterStride或者InnerStride,默认为是连续的,也就是没有步幅。所谓的OuterStride指的是,如果我们构造的矩阵为列优先(column-major),那么每两个连续的列之间的指针就会跳跃一个步幅,如果为InnerStride,那么矩阵或者向量内部每两个连续的元素之间就会跳跃一个步幅。

Map有三种构造函数:       

Map (PointerArgType dataPtr, const StrideType &stride=StrideType())
 
Map (PointerArgType dataPtr, Index size, const StrideType &stride=StrideType())
 
Map (PointerArgType dataPtr, Index rows, Index cols, const StrideType &stride=StrideType())

dataPtr:指向数据的指针

StrideType:步幅的类型

size:用于构造向量时指定大小

rows,cols:几行几列

下面看例程:

int array[9];
for(int i = 0; i < 9; ++i) array[i] = i;
cout << Map<Matrix3i>(array) << endl;

output:
0 3 6
1 4 7
2 5 8

带StrideType的:

int array[12];
for(int i = 0; i < 12; ++i) array[i] = i;
cout << Map<VectorXi, 0, InnerStride<2> >
         (array, 6) // the inner stride has already been passed as template parameter
     << endl;

output:
 0
 2
 4
 6
 8
10
int array[12];
for(int i = 0; i < 12; ++i) array[i] = i;
cout << Map<MatrixXi, 0, OuterStride<> >(array, 3, 3, OuterStride<>(4)) << endl;

output:
 0  4  8
 1  5  9
 2  6 10
int data[] = {1,2,3,4,5,6,7,8,9};
Map<RowVectorXi> v(data,4);
cout << "The mapped vector v is: " << v << "\n";
new (&v) Map<RowVectorXi>(data+4,5);
cout << "Now v is: " << v << "\n";

output:
The mapped vector v is: 1 2 3 4
Now v is: 5 6 7 8 9

 

  • 8
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值