整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html

块操作

块是matrix或array中的矩形子部分。

使用块

函数.block(),有两种形式

operation 构建一个动态尺寸的block 构建一个固定尺寸的block
起点(i,j)块大小(p,q) .block(i,j,p,q) .block< p,q >(i,j)

Eigen中,索引从0开始。

两个版本都可以用于固定尺寸和动态尺寸的matrix/array。功能是等价的,只是固定尺寸的版本在block较小时速度更快一些。

int main()
{
Eigen::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

作为左值

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

行和列

Operation Method
ith row matrix.row(i)
jth colum matrix.col(j)
int main()
{
Eigen::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 the 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 the third column, the matrix m is:
1 2 6
4 5 18
7 8 30

角相关操作

operation dynamic-size block fixed-size block
左上角p\*q matrix.topLeftCorner(p,q); matrix.topLeftCorner< p,q >();
左下角p\*q matrix.bottomLeftCorner(p,q); matrix.bottomLeftCorner< p,q >();
右上角p\*q matrix.topRightCorner(p,q); matrix.topRightCorner< p,q >();
右下角p\*q matrix.bottomRightCorner(p,q); matrix.bottomRightCorner< p,q >();
前q行 matrix.topRows(q); matrix.topRows< q >();
后q行 matrix.bottomRows(q); matrix.bottomRows< q >();
左p列 matrix.leftCols(p); matrix.leftCols< p >();
右p列 matrix.rightCols(p); matrix.rightCols< p >();
int main()
{
Eigen::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

vectors的块操作

operation dynamic-size block fixed-size block
前n个 vector.head(n); vector.head< n >();
后n个 vector.tail(n); vector.tail< n >();
i起始的n个元素 vector.segment(i,n); vector.segment< n >(i);

Eigen教程(5)的更多相关文章

  1. Eigen教程(7)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 归约.迭代器和广播 归约 在Eigen中,有些函数可以统计matrix/array的 ...

  2. Eigen教程(6)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 高级初始化方法 本篇介绍几种高级的矩阵初始化方法,重点介绍逗号初始化和特殊矩阵(单位 ...

  3. Eigen教程(11)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 存储顺序 对于矩阵和二维数组有两种存储方式,列优先和行优先. 假设矩阵: 按行优先存 ...

  4. Eigen教程(9)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Eigen并没有为matrix提供直接的Reshape和Slicing的API,但是 ...

  5. Eigen教程(10)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 混淆 在Eigen中,当变量同时出现在左值和右值,赋值操作可能会带来混淆问题.这一篇 ...

  6. Eigen教程(8)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 原生缓存的接口:Map类 这篇将解释Eigen如何与原生raw C/C++ 数组混合 ...

  7. Eigen教程(4)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Array类和元素级操作 为什么使用Array 相对于Matrix提供的线性代数运算 ...

  8. Eigen教程(3)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 矩阵和向量的运算 提供一些概述和细节:关于矩阵.向量以及标量的运算. 介绍 Eige ...

  9. Eigen教程(2)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Matrix类 在Eigen,所有的矩阵和向量都是Matrix模板类的对象,Vect ...

随机推荐

  1. 【Servlet】web.xml中url-pattern的用法

    目录结构: contents structure [+] url-pattern的三种写法 servlet匹配原则 filter匹配原则 语法错误的后果 参考文章 一.url-pattern的三种写法 ...

  2. python学习笔记——爬虫学习中的重要库urllib

    1 urllib概述 1.1 urllib库中的模块类型 urllib是python内置的http请求库 其提供了如下功能: (1)error 异常处理模块 (2)parse url解析模块 (3)r ...

  3. Oracle 12C -- 扩展varchar2、nvarchar2、和raw数据类型的大小限制

    在12C中,varchar2,nvarchar2和raw类型从之前的4K扩展到32K 升级到12C后,参数max_string_size默认值是standard,即不改变varchar2.nvarch ...

  4. 【转】Java 有值类型吗?

    Java 有值类型吗? 有人看了我之前的文章『Swift 语言的设计错误』,问我:“你说 Java 只有引用类型(reference type),但是根据 Java 的官方文档,Java 也有值类型( ...

  5. MFC中无标题栏窗口的移动

    原文链接: http://blog.sina.com.cn/s/blog_6288219501015dwa.html   移动标准窗口是通过用鼠标单击窗口标题条来实现的,但对于没有标题条的窗口,就需要 ...

  6. 如何修改电脑的本地网卡(非笔记本无限网卡)的mac地址

    计算机---设备管理器--找到对应的本地网卡---右键属性-----高级----网络地址

  7. HTML自定义对象与属性探究(谷歌,火狐,IE9浏览器没问题)

    1.自定义标签 <zqz>asdas</zqz> <style> zqz{ color:red; } </style> 页面变色 2.自定义标签的hov ...

  8. 正则和xpath在网页中匹配字段的效率比较

    1. 测试页面是  https://www.hao123.com/,这个是百度的导航 2. 为了避免网络请求带来的差异,我们把网页下载下来,命名为html,不粘贴其代码. 3.测试办法: 我们在页面中 ...

  9. iOS常用RGB颜色的色值表

    常用RGB颜色表  R G B 值 R G B 值 R G B 值 黑色 0 0 0 #000000 黄色 255 255 0 #FFFF00 浅灰蓝色 176 224 230 #B0E0E6 象牙黑 ...

  10. WPF 控件事件的一个小坑…

    最近想判断一下 Slider 是由鼠标点击而改变值,还是由程序内部代码改变的值,发现鼠标的各种事件比如 MouseDown.MouseUp.MouseLeftButtonDown 什么的,都没有任何反 ...