整理下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. opestack keystone 深入

    一.概述 keystone 有两个endpoint端口,一个35357,用于管理,只有admin_role可以使用.一个是5000, 用于业务: 二.keystone中的路由 解析url,然后获取后端 ...

  2. AndroidStudio OpenCv的配置,不用安装opencv manager

    按照以下操作步骤配置并测试了,没问题. 下载OpenCV sdk for Android,解压(我的解压地址是F:\OpenCV-android-sdk) 1)新建项目项目,取名为Opencvtest ...

  3. Spring-Boot服务注册与发现

    关于Eureka服务注册与发现的示例可以参见:http://blog.didispace.com/springcloud1/ 服务注册管理器原理如下图所示: 1.建立eureka服务器 @Enable ...

  4. Android Studio找不到FragmentActivity类

    右击项目——>open module settings——>选择第五个选项卡“Dependencies”——>点击加号——>选择第一个Library dependency——& ...

  5. C语言函数实现的另类方法

    在前面看过那个BT的Javascript程序后,我们来看一个C语言的,相信大家还记得输出从1到1000的数最后的那个示例,本站还有很多这样的示例,如:变态的hello word,如何教新手编程,还有恐 ...

  6. 【转】对 Parser 的误解

    一直很了解人们对于parser的误解,可是一直都提不起兴趣来阐述对它的观点.然而我觉得是有必要解释一下这个问题的时候了.我感觉得到大部分人对于parser的误解之深,再不澄清一下,恐怕这些谬误就要写进 ...

  7. HTML5无刷新修改URL

    HTML5新添加了两个api分别是pushState和replaceState,DOM中的window对象通过window.history方法提供了对浏览器历史记录的读取,可以在用户的访问记录中前进和 ...

  8. mysql 多个字段 order by

    mysql中,我们可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列. 默认情况下,它是按升序排列. order by 后可加2个字段,用英文逗号隔开, 如A用升序, B降序,SQ ...

  9. php获取图片RGB颜色值的例子

    php获取图片RGB颜色值的例子 很多图片站点都会根据用户上传的图片检索出图片的主要颜色值,然后在通过颜色搜索相关的图片. 之前按照网上的方法将图片缩放(或者马赛克)然后遍历每个像素点,然后统计处RG ...

  10. 【Unity】第5章 3D坐标系和天空盒

    分类:Unity.C#.VS2015 创建日期:2016-04-20 一.简介 这一张主要介绍3D坐标系的基础知识以及各种形状的天空盒. 二.示例 本章的示例都在ch05Demos工程下.