简介

Eigen 中Matrix/Array提供了.block()来进行block区块操作,这是面向系数提供的操作功能。

语法

Eigen中提供了2种语法,针对产生的结果是一致的。但存在性能上的不同,任何时候,使用fixed-size模式都会得到更好地性能优化。

分类 语法 说明
dynamic-size matrix.block(i,j,p,q); 执行时才知道维度大小(p,q)。
fixed-size matrix.block<p,q>(i,j); 编译时指定维度大小(p,q)。

解释: 区块的维度大小尺寸:(p,q), 起始的位置:(i,j)。同样的,位置索引从0开始。

查看一个简单的示例:

//matrix_block1.cpp#include <Eigen/Dense>
#include <iostream>using namespace Eigen;
using namespace std;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 << "matrix m:" << endl << m <<endl<<endl;cout<<"-----------------"<<endl;cout << "Block in the middle -- m.block<2,2>(1,1)" << endl;cout << m.block<2,2>(1,1) << endl << endl;cout<<"-----------------"<<endl;for (int i = 1; i <= 3; ++i){cout << "Get from (0,0), Block of size " << i << "x" << i << endl;cout << m.block(0,0,i,i) << endl << endl;}cout<<"-----------------"<<endl;for (int i = 1; i <= 3; ++i){cout << "Get from (1,1), Block of size " << i << "x" << i << endl;cout << m.block(1,1,i,i) << endl << endl;}
}

执行的结果:

$ g++   -I /usr/local/include/eigen3 matrix_block1.cpp -o matrix_block1
$
$ ./matrix_block1
matrix m:1  2  3  45  6  7  89 10 11 12
13 14 15 16-----------------
Block in the middle -- m.block<2,2>(1,1)6  7
10 11-----------------
Get from (0,0), Block of size 1x1
1Get from (0,0), Block of size 2x2
1 2
5 6Get from (0,0), Block of size 3x31  2  35  6  79 10 11-----------------
Get from (1,1), Block of size 1x1
6Get from (1,1), Block of size 2x26  7
10 11Get from (1,1), Block of size 3x36  7  8
10 11 12
14 15 16

block()不仅可以用于右值,也可以用于左值。下面来个Array的简单示例。

//matrix_block2.cpp#include <Eigen/Dense>
#include <iostream>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;
}

此示例中,在一个4X4的常量二维数组中,修改(2,2,1,1)区块的系数,此次修改操作对应大小指定为1X1了,取值来源区块为2X2数组。

执行结果如下,最后划线的部分是最后一次block区块替换的部分:

$ g++   -I /usr/local/include/eigen3 matrix_block2.cpp -o matrix_block2
$
$ ./matrix_block2
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.6Here 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.6Here 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中提供了更好地API方法col(), row()

  • matrix.row(i) : 第 i 行;
  • matrix.col(j) : 第 j 列;

示例:

//matrix_block3.cpp
#include <Eigen/Dense>
#include <iostream>using namespace std;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 m.col(2) += 3 * m.col(0), the matrix m is:\n";cout << m << endl;
}

执行:

$ g++   -I /usr/local/include/eigen3 matrix_block3.cpp -o matrix_block3
promote:eigen david$ ./matrix_block3
Here is the matrix m:
1 2 3
4 5 6
7 8 9
2nd Row: 4 5 6
After m.col(2) += 3 * m.col(0), the matrix m is:1  2  64  5 187  8 30

边角操作

有时候,需要对矩阵的边角进行操作。Matrix提供了对应的方法函数,可以很变量地进行操作。

target 动态 固定尺寸
左上 matrix.topLeftCorner(p,q); matrix.topLeftCorner<p,q> ();
左下 matrix.bottomLeftCorner(p,q); matrix.bottomLeftCorner<p,q> ();
右上 matrix.topRightCorner(p,q); matrix.topRightCorner<p,q> ();
右上 matrix.bottomRightCorner(p,q); matrix.bottomRightCorner<p,q> ();
顶部q行 matrix.topRows(q); matrix.topRows ();
底部q行 matrix.bottomRows(q); matrix.bottomRows ();
左边p列 matrix.leftCols§; matrix.leftCols

();

右边p列 matrix.rightCols(q); matrix.rightCols ();

向量的block块操作

准对向量、一维数组,Eigen还提供了一组特别的块操作。

操作 Dynamic 模式 fixed_size 模式
头部n个 vector.head(n); vector.head();
尾部n个 vector.tail(n); vector.tail();
自i起始的n个 vector.segment(i,n); vector.segment(i);

示例

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

执行:

$ g++   -I /usr/local/include/eigen3 matrix_block4.cpp -o matrix_block4
promote:eigen david$ ./matrix_block4
v.head(3) =
1
2
3v.tail<3>() =
4
5
6after 'v.segment(1,4) *= 2', v =1468
106

Eigen入门之密集矩阵 4 - 块操作相关推荐

  1. Eigen入门之密集矩阵 3 - Array操作

    简介 在Eigen内,有Matrix,vector进行线性代数的相关运算,但也需要执行对矩阵内的系数的相关操作时,这是正常的功能需求.Eigen中的Array类就是满足此需求的. Array 定义 和 ...

  2. Eigen入门之密集矩阵 7 - Map class:连接Eigen与C++的数据

    简介 本文介绍一下Dense Matrix如何与c/C++的数组进行交互操作,这在引入其他的库中的vector向量和矩阵到Eigen中时要使用到的技术. 有时,你有一些定义好的数据,可能是数组,你需要 ...

  3. Eigen入门之密集矩阵 6 - Reductions, visitors and broadcasting

    简介 本文介绍一下Dense Matrix的3中操作: reduction, visitor, broadcasting. 归约计算reduction. Eigen的归约计算是这样的一类计算,它是对矩 ...

  4. Eigen入门之密集矩阵 2-- Matrix及Vector的计算方法

    简介 Eigen内的Matrix和Vector提供了类似C++的运算符,如+,-,*:也提供了编程的函数方法,如点乘和叉乘的dot(), cross(),如此等等. 在Eigen的Matrix类,代表 ...

  5. Eigen入门之密集矩阵 9 - 别名混乱Aliasing

    简介 别名混乱Aliasing是指在赋值表达式中,一个Eigen对象(矩阵.数组.向量)同时出现在左值和右值表达式中,比如v = v*2; m = m.transpose();; 别名混乱会引起错误, ...

  6. Eigen入门之密集矩阵 10 - 矩阵的行优先及列优先存储

    简介 本篇介绍Eigen中矩阵及二维数组的系数存储顺序–行优先及列优先,已经如何指定优先顺序. 行优先(row-majoe). 列优先(column-majoe) 矩阵的系数条目组成了一个二维的结构, ...

  7. Eigen入门之密集矩阵 8 - resharp slicing切片

    简介 Eigen还没有提供resharp或者slicing的处理函数,但是,可以使用Map 类来实现这些功能. 实现resharp 操作Resharp及修改Matrix的维度大小,而其系数保持不变.R ...

  8. Eigen入门之密集矩阵 5 - 再谈Matrix初始化

    简介 这里将讨论一下高级些的矩阵初始化方法. comma-initializer 逗号初始化器 comma-initializer方法很简单,可以一下把矩阵/向量的系数全部设置完.语法很简单,使用逗号 ...

  9. Eigen入门之密集矩阵 1 -- 类Matrix介绍

    简介 本篇介绍Eigen中的Matrix类.在Eigen中,矩阵和向量的类型都用Matrix来表示.向量是一种特殊的矩阵,其只有一行或者一列. Matrix构造 在Matrix.h中,定义了Matri ...

最新文章

  1. win10 linux子系统
  2. svnserve.conf - snvserve 的仓库配置文件
  3. a标签连接空标签的方法
  4. 设计模式 生成器_生成器设计模式的应用
  5. MATLAB rolcus函数,自动控制原理实验报告 .doc
  6. javascript 技巧总结积累1-108条(正在积累中)
  7. 自动布局和view 设置frame同时有效
  8. GaN制备Micro-led(二)——光子晶体倒装 Micro-LED 制备的关键工艺(纳米压印光刻、干法刻蚀、介质薄膜沉积、物理气相沉积)
  9. 大学计算机案例教程旧照片修复,「教你一招」使用自动软件修复老照片
  10. 苹果MFi认证协处理器(MFI337S3959)公钥证书分析
  11. 安卓毕业设计源码基于Uniapp+SSM实现的校园心理健康APP
  12. 年包150万的腾讯程序员,深圳房产一千万,同学聚会只能排名第16!
  13. 赚多多V10自动任务网抢单源码+会员自营版+教程
  14. 次微分(subdifferential)
  15. java电商商品搜索_Java生鲜电商平台-搜索模块的设计与架构
  16. linu安装python走到300就不动了_linux centos 安装python3.7报错会在load avg: 0.63 [307/416] test_socket卡住 解决办法...
  17. 庄伟雄:基于移动互联网的供应链管理
  18. 常用交换机的配置命令
  19. [屌丝PM]做一个网络“拾穗者”(2)
  20. 三星p7510 android5,三星平板电脑p7510测评报价及刷机教程大放送【图文】

热门文章

  1. 实现 VUE 中 MVVM - step10 - Computed
  2. mysql的主从分离_Mysql的主从分离配置
  3. sqlserver和mysql运营_SQLServer和MySql的区别总结
  4. 微软小娜 服务器连不上网,Win10 64位联网状态下微软小娜无法连接网络
  5. 对象 普通po转_厦门2020年转学怎么转?需要什么材料?你想知道的答案都在这!......
  6. 桌面在计算机领域常用来指,桌面在计算机领域常用来指什么
  7. php表单验证并使值变化,php – Zend_Form手动设置和验证字段值
  8. Trouble Sort CodeForces - 1365B(思维)
  9. python关闭线程daemon_python中threading开启关闭线程操作
  10. mysql libs 5.1.71_用python创建数据库监控平台(1)安装MySQL5.7