1 子矩阵操作简介

子矩阵操作又称块操作,在矩阵运算中,子矩阵的提取和操作应用也十分广泛。因此Eigen中也提供了相关操作的方法。提取的子矩阵在操作过程中既可以用作左值也可以用作右值。

2 块操作的一般使用方法

在Eigen中最基本的快操作运算是用.block()完成的。提取的子矩阵同样分为动态大小和固定大小。

|块操作|构建动态大小子矩阵|构建固定大小子矩阵|

|:-|:-|

|提取块大小为(p,q),起始于(i,j)|matrix.block(i,j,p,q)|matrix.block<p,q>(i,j)|

同样需要注意的是在Eigen中,索引是从0开始。所有的操作方法都可以适用于Array.同样使用固定大小的操作方式在小型矩阵运算时更加的快,但要求在编译时就要知道矩阵的大小。

下面是一个使用示例:

#include <iostream>
#include "Eigen/Dense" using namespace std;
using namespace Eigen; int main()
{
MatrixXf m(4,4);
m<< 1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16;
cout<<"Block in the middle"<<endl;
cout<<m.block<2,2>(1,1)<<endl<<endl;
for(int i = 1;i <= 3;++i)
{
cout<<"Block of size "<<i<<"x"<<i<<endl;
cout<<m.block(0,0,i,i)<<endl<<endl;
}
}

执行结果如下:

Block in the middle
6 7
10 11 Block of size 1x1
1 Block of size 2x2
1 2
5 6 Block of size 3x3
1 2 3
5 6 7
9 10 11

上面的示例中,.block()被应用为左值操作,即从中读取数据。事实上,它也可以被用作为右值操作,即也可往里面写入数据。下面是一个右值应用实例。

#include <iostream>
#include "Eigen/Dense" using namespace std;
using namespace Eigen; int main()
{
Array22f m;
m<< 1,2,
3,4;
Array44f a = Array44f::Constant(0.6);
cout<<"Here is the array a:"<<endl<<a<<endl<<endl;
a.block<2,2>(1,1) = m;
cout<<"Here is now a with m copied into its central 2x2 block:"<<endl<<a<<endl<<endl;
a.block(0,0,2,3) = a.block(2,1,2,3);
cout<<"Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:"<<endl<<a<<endl<<endl;
}

执行结果如下:

Here is the array a:
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6 Here is now a with m copied into its central 2x2 block:
0.6 0.6 0.6 0.6
0.6 1 2 0.6
0.6 3 4 0.6
0.6 0.6 0.6 0.6 Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:
3 4 0.6 0.6
0.6 0.6 0.6 0.6
0.6 3 4 0.6
0.6 0.6 0.6 0.6

.block()方法是一种非常通用的块操作方法。除了这个通用的方法以外,Eigen中还为一些常用的特殊操作提供了特殊的函数。从运行速度的方面来看,你应该在编译阶段尽可能的提供更多的信息。比如,如果你需要操作的块是一个列,那么你可以使用.col()函数。这样Eigen可以得知这个信息以便进行更多的优化。

这些特殊操作方法总结如下。

3 行子式和列子式

我们可以使用.col()和.row()方法来操作或者提取一个列或者行。

块操作 方法
第i行 matrix.row(i);
第j列 matrix.col(j);

下面是一个使用示例:

#include <iostream>
#include "Eigen/Dense" using namespace std;
using namespace Eigen; int main()
{
MatrixXf m(3,3);
m<< 1,2,3,
4,5,6,
7,8,9;
cout<<"Here is the matrix m:"<<endl<<m<<endl;
cout<<"2nd Row:"<<m.row(1)<<endl;
m.col(2) += 3*m.col(0);
cout<<"After adding 3 times the first column into third column,the matrix m is:\n";
cout<<m<<endl;
}

执行结果如下:

Here is the matrix m:
1 2 3
4 5 6
7 8 9
2nd Row:4 5 6
After adding 3 times the first column into third column,the matrix m is:
1 2 6
4 5 18
7 8 30

4 边角子矩阵

Eigen提供了从边角开始提取子矩阵的方法,比如.topLeftCorner()表示从左上角开始提取子矩阵。这些操作总结如下:

块操作 动态矩阵版本 固定矩阵版本
左上角pxq matrix.topLeftCorner(p,q); matrix.topLeftCorner<p,q>();
左下角pxq matrix.bottomLeftCorner(p,q); matrix.bbottomLeftCorner<p,q>();
右上角pxq matrix.topRightCorner(p,q); matrix.topRightCorner<p,q>();
右下角pxq matrix.bottomRightCorner(p,q); matrix.bottomRightCorner<p,q>();
前p行 matrix.topRows(p); matrix.topRows<p>();
后p行 matrix.bottomRows(p); matrix.bottomRows<p>();
前q列 matrix.leftCols(q); matrix.leftCols<q>();
后q列 matrix.rightCols(q); matrix.rightCols<q>();

下面是一个使用示例:

#include <iostream>
#include "Eigen/Dense" using namespace std;
using namespace Eigen; int main()
{
Matrix4f m;
m<< 1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16;
cout<<"m.leftCols(2)="<<endl<<m.leftCols(2)<<endl<<endl;
cout<<"m.bottomRows<2>()="<<endl<<m.bottomRows<2>()<<endl<<endl;
m.topLeftCorner(1,3) = m.bottomRightCorner(3,1).transpose();
cout<<"After assignment,m="<<endl<<m<<endl;
}

执行结果如下:

m.leftCols(2)=
1 2
5 6
9 10
13 14 m.bottomRows<2>()=
9 10 11 12
13 14 15 16 After assignment,m=
8 12 16 4
5 6 7 8
9 10 11 12
13 14 15 16

5 向量的子向量操作

Eigen中同样也为向量提供了一些子式的操作方法,总结如下:

块操作 固定向量版本 动态向量版本
前n个元素 vector.head(n); vector.head<n>();
后n个元素 vector.tail(n); vector.tail<n>();
从i开始取n个元素 vector.segment(i,n) vector.segment<n>(i);

再次说明一下,所有对矩阵的操作同样适用于Array,所有对列向量的操作同样适用于行向量。

下面是一个使用示例:

#include <iostream>
#include "Eigen/Dense" using namespace std;
using namespace Eigen; int main()
{
ArrayXf v(6);
v<<1,2,3,4,5,6;
cout<<"v.head(3)="<<endl<<v.head(3)<<endl<<endl;
cout<<"v.tail<3>()="<<endl<<v.tail<3>()<<endl<<endl;
v.segment(1,4) *= 2;
cout<<"after 'v.segment(1,4) *= 2',v="<<endl<<v<<endl;
}

执行结果如下:

v.head(3)=
1
2
3 v.tail<3>()=
4
5
6 after 'v.segment(1,4) *= 2',v=
1
4
6
8
10
6

Eigen子矩阵操作的更多相关文章

  1. eigen矩阵操作练习

    // // Created by qian on 19-7-16. // /* 相机位姿用四元数表示 q = [0.35, 0.2, 0.3, 0.1] x,y,z,w * 注意:输入时Quatern ...

  2. [uva11992]Fast Matrix Operations(多延迟标记,二维线段树,区间更新)

    题目链接:https://vjudge.net/problem/UVA-11992 题意:n*m的矩阵,每次对一个子矩阵操作,有三种操作:加x,设置为x,查询.查询返回子矩阵和.最小值.最大值 n很小 ...

  3. 从零开始一起学习SLAM | 三维空间刚体的旋转

    刚体,顾名思义,是指本身不会在运动过程中产生形变的物体,如相机的运动就是刚体运动,运动过程中同一个向量的长度和夹角都不会发生变化.刚体变换也称为欧式变换. 视觉SLAM中使用的相机就是典型的刚体,相机 ...

  4. UVA 11992 Fast Matrix Operations (降维)

    题意:对一个矩阵进行子矩阵操作. 元素最多有1e6个,树套树不好开(我不会),把二维坐标化成一维的,一个子矩阵操作分解成多条线段的操作. 一次操作的复杂度是RlogC,很容易找到极端的数据(OJ上实测 ...

  5. POJ 2155:Matrix 二维树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21757   Accepted: 8141 Descripti ...

  6. Eigen库实现简单的旋转、平移操作

    本来课程要求用GUI界面来实现Eigen的旋转.平移操作的,但是接触GUI编程时间太短,虽然要求很简单,但是做了几天还是没有完成.就把命令行下面的简单的贴一下吧. main.cpp #include ...

  7. Eigen 学习之块操作

    Eigen 为 Matrix .Array 和  Vector提供了块操作方法.块区域可以被用作 左值 和 右值.在Eigen中最常用的块操作函数是 .block() . block() 方法的定义如 ...

  8. Eigen介绍及简单使用

    博客转载自:https://blog.csdn.net/fengbingchun/article/details/47378515 Eigen是可以用来进行线性代数.矩阵.向量操作等运算的C++库,它 ...

  9. MATLAB常用操作

    1.点乘,点除,点乘方 点乘(对应元素相乘),必须同维或者其中一个是标量,a.*b 点除,a.\b表示矩阵b的每个元素除以a中对应元素或者除以常数a,a./b表示常数a除以矩阵b中每个元素或者矩阵a除 ...

随机推荐

  1. MySQL 命令(导出数据):mysqldump

    官方网址:https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html

  2. C语言数组指针

    C语言中的数组指针与指针数组: ·数组指针一.区分 首先我们需要了解什么是数组指针以及什么是指针数组,如下: int *p[5];int (*p)[5];数组指针的意思即为通过指针引用数组,p先和*结 ...

  3. FPGA 关于中间环节生成时钟的约束---Generated clocks

    FPGA 关于中间环节生成时钟的约束---Generated clocks FPGA 的生成时钟,有一个主时钟源,在定义的之后,需要指明主时钟源. 生成时钟主要定义的是:分频,倍频,相移等 二分频时钟 ...

  4. ORACLE的impdp和expdp命令

    使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用, ...

  5. 【python】变量的赋值、深浅拷贝

    python——赋值与深浅拷贝 https://www.cnblogs.com/Eva-J/p/5534037.html 啥都不说,看这个博主的文章!

  6. 开机自启动 centos 7

    开机自启动,写入必要的命令即可.vim /etc/rc.d/rc.local

  7. web--webstorm的一些常用快捷键

    Webstorm的一些常用快捷键   下面是Webstorm的一些常用快捷键: Alt+回车 导入包,自动修正 1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录 ...

  8. MongoDB复制集技术

    复制集搭建 没毛病: https://www.cnblogs.com/nicolegxt/p/6841442.html?utm_source=itdadao&utm_medium=referr ...

  9. vue源码核心部分

    1.模板编译   初始化时做的:template ==parse()==>ASTtree ==generate()==>render函数  ==> mount(调用dom方法) 每次 ...

  10. javaweb项目编译错误

    Eclipse Maven 开发一个 jee 项目时,编译时遇到以下错误:Description Resource Path Location TypeDynamic Web Module 3.0 r ...