整理下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较小时速度更快一些。

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

输出

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

作为左值

  1. int main()
  2. {
  3. Array22f m;
  4. m << 1,2,
  5. 3,4;
  6. Array44f a = Array44f::Constant(0.6);
  7. cout << "Here is the array a:" << endl << a << endl << endl;
  8. a.block<2,2>(1,1) = m;
  9. cout << "Here is now a with m copied into its central 2x2 block:" << endl << a << endl << endl;
  10. a.block(0,0,2,3) = a.block(2,1,2,3);
  11. cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:" << endl << a << endl << endl;
  12. }

输出

  1. Here is the array a:
  2. 0.6 0.6 0.6 0.6
  3. 0.6 0.6 0.6 0.6
  4. 0.6 0.6 0.6 0.6
  5. 0.6 0.6 0.6 0.6
  6. Here is now a with m copied into its central 2x2 block:
  7. 0.6 0.6 0.6 0.6
  8. 0.6 1 2 0.6
  9. 0.6 3 4 0.6
  10. 0.6 0.6 0.6 0.6
  11. Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:
  12. 3 4 0.6 0.6
  13. 0.6 0.6 0.6 0.6
  14. 0.6 3 4 0.6
  15. 0.6 0.6 0.6 0.6

行和列

Operation Method
ith row matrix.row(i)
jth colum matrix.col(j)
  1. int main()
  2. {
  3. Eigen::MatrixXf m(3,3);
  4. m << 1,2,3,
  5. 4,5,6,
  6. 7,8,9;
  7. cout << "Here is the matrix m:" << endl << m << endl;
  8. cout << "2nd Row: " << m.row(1) << endl;
  9. m.col(2) += 3 * m.col(0);
  10. cout << "After adding 3 times the first column into the third column, the matrix m is:\n";
  11. cout << m << endl;
  12. }

输出

  1. Here is the matrix m:
  2. 1 2 3
  3. 4 5 6
  4. 7 8 9
  5. 2nd Row: 4 5 6
  6. After adding 3 times the first column into the third column, the matrix m is:
  7. 1 2 6
  8. 4 5 18
  9. 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 >();
  1. int main()
  2. {
  3. Eigen::Matrix4f m;
  4. m << 1, 2, 3, 4,
  5. 5, 6, 7, 8,
  6. 9, 10,11,12,
  7. 13,14,15,16;
  8. cout << "m.leftCols(2) =" << endl << m.leftCols(2) << endl << endl;
  9. cout << "m.bottomRows<2>() =" << endl << m.bottomRows<2>() << endl << endl;
  10. m.topLeftCorner(1,3) = m.bottomRightCorner(3,1).transpose();
  11. cout << "After assignment, m = " << endl << m << endl;
  12. }

输出

  1. m.leftCols(2) =
  2. 1 2
  3. 5 6
  4. 9 10
  5. 13 14
  6. m.bottomRows<2>() =
  7. 9 10 11 12
  8. 13 14 15 16
  9. After assignment, m =
  10. 8 12 16 4
  11. 5 6 7 8
  12. 9 10 11 12
  13. 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. 会动的Tabbar

    项目搭建 一.设计模式首先呢,小Q采用传统的MVC的设计模式,优点我们再来啰嗦一下啊:1.多个视图可以对应一个模型.按MVC设计模式,一个模型对应多个视图,可以减少代码的复制及代码的维护量,一旦模型发 ...

  2. sqlserver 2008 开启CLR

    Common language runtime (CLR) 特性支持在sql server中编写和执行.net的存储过程.触发器.和函数但是要想执行CLR代码,首先要开启CLR特性 1.查看CLR特性 ...

  3. Loader Lock引起的一个Bug

    在Windows中,让程序模块化实现的一种方式,就是让事实上现为动态链接库. 然后在主程序启动的时候隐式或者显示的去载入动态链接库.可是假设不恰当的编写动态链接库的DllMain函数,将会引起意想不到 ...

  4. 替代crontab,任务计划统一集中管理系统cronsun简介

    一.背景 crontab 是 Linux 系统里面最简单易用的定时任务管理工具,相信绝大多数开发和运维都用到过.在咱们公司,很多业务系统的定时任务都是通过 crontab 来定义的,时间长了后会发现存 ...

  5. Android Support Library 23.2介绍(翻译自官方文档)

    Android Support Library 23.2 (译者注:本文标注了部分文字链接,但须要***,要查看全部链接.请查看sukey=014c68f407f2d3e181b6b5e665f26a ...

  6. update关联其他表批量更新数据-跨数据库-跨服务器Update时关联表条件更新

    1.有时在做项目时会有些期初数据更新,从老系统更新到新系统.如果用程序循环从老系统付给新系统. 2.有时在项目中需要同步程序,或者自动同步程序时会有大量数据更新就可能用到如下方法了. 3.为了做分析, ...

  7. win8.1安装驱动出现“文件的哈希值不在指定的目录”的解决办法[zz]

    1.鼠标移到右下角,点击“设置”,再点击“更改电脑设置”2.点击最后一个“更新和回复”,再点击“恢复”3.点击“恢复”之后,在右边点击高级启动下面的“重新启动”4.等一会会出现几个选项,点击“疑难解答 ...

  8. 网站启用SSL后重启Nginx提示 Enter PEM Pass Phrase:需要输入密码

    ​最近在玩 STARTSSL 感觉个人站点使用这个SSL差不多也够用了.真正商用的SSL当然也可以自行购买. 我个人是为了防止数据中间被抓走,所以用了startssl 也基本就够用了. 转回正题,st ...

  9. Spring 注解@Component,@Service,@Controller,@Repository

    Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...

  10. Java:多线程,CountDownLatch同步器

    1. 背景 CountDownLatch类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown( ...