Eigen教程(5)
整理下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)的更多相关文章
- Eigen教程(7)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 归约.迭代器和广播 归约 在Eigen中,有些函数可以统计matrix/array的 ...
- Eigen教程(6)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 高级初始化方法 本篇介绍几种高级的矩阵初始化方法,重点介绍逗号初始化和特殊矩阵(单位 ...
- Eigen教程(11)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 存储顺序 对于矩阵和二维数组有两种存储方式,列优先和行优先. 假设矩阵: 按行优先存 ...
- Eigen教程(9)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Eigen并没有为matrix提供直接的Reshape和Slicing的API,但是 ...
- Eigen教程(10)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 混淆 在Eigen中,当变量同时出现在左值和右值,赋值操作可能会带来混淆问题.这一篇 ...
- Eigen教程(8)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 原生缓存的接口:Map类 这篇将解释Eigen如何与原生raw C/C++ 数组混合 ...
- Eigen教程(4)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Array类和元素级操作 为什么使用Array 相对于Matrix提供的线性代数运算 ...
- Eigen教程(3)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 矩阵和向量的运算 提供一些概述和细节:关于矩阵.向量以及标量的运算. 介绍 Eige ...
- Eigen教程(2)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Matrix类 在Eigen,所有的矩阵和向量都是Matrix模板类的对象,Vect ...
随机推荐
- linux达人养成计划学习笔记(二)—— 文件查找命令
一.locate命令 1.命令格式: locate 文件名 2.locate在后台数据库中按文件名搜索,速度快,locate命令所搜索的后台数据库 /var/lib/mlocate 3.后台数据库跟新 ...
- 为Magento1.5新增会员注册字段(转)
第一步.新建一个模块,在app/etc/modules/目录下新建文件Shuishui_Customer.xml <config> <modules> <Shuishui ...
- 定制jQuery File Upload为微博式单文件上传
日志未经声明,均为AlloVince原创.版权采用『 知识共享署名-非商业性使用 2.5 许可协议』进行许可. jQuery File Upload是一个非常优秀的上传组件,主要使用了XHR作为上传方 ...
- 算法中的 log 到底是什么?
之前一直不解为何算法中经常会看到 log 今天看<数据结构与算法分析 Java 语言描述>(第 3 版)2.4.3 节 求最大子序列和的分治算法实现时才注意到原因 翻看第 29 页的最后一 ...
- SmartUpload类实现上传和下载
实现文件的上传与下载,可以使用Java的I/O流的类来实现,也可以使用专业的上传.下载组件.这些组件提供了现成的类,程序员只需调用这些类中的方法即可实现文件的上传与下载.本章将向读者介绍如何应用jsp ...
- AaronYang的留言板
^_^很开心能在这里遇到你,我是ay,英文名叫aaronyang,真名叫杨洋,安徽六安的,有老乡吗?这里的文章几乎都是我原创的,要不然就是收集别人的好的文章,自己再整理下与大家分享.绝对希望原创,本站 ...
- python selenium expected_conditions使用实例
今天正好虫师问到selenium python binding中support.expected_conditions的用法,顺手总结了一下,希望对大家有所帮助. 场景 Expected Condit ...
- appium简明教程(3)——appium的安装windows版
appium的哲学里有一条就是不重新发明轮子.同样,官方已经有明确的安装步骤了,因此在这里纯属搬砖. 原文地址 感谢testerhome的辛勤翻译. 本文版权归乙醇所有,欢迎转载,但请注明作者与出处, ...
- rdlc 分页操作和分页统计
1. 工具箱中拖一个列表过来,设置 列表-->行组-->组属性常规-->组表达式=Int((RowNumber(Nothing)-1)/10)分页符-->勾选在组的结尾. 2. ...
- stm8 时钟输出引脚
CLK_CCO引脚是STM8的时钟输出引脚,若设置该脚输出主时钟Fmaster,时钟输出寄存器可以进行如下操作 CLK->CCOR=0X19;