c++ Eigen之block

这篇博客详细介绍了Eigen库中矩阵块操作的方法,包括动态大小和固定大小的块选择,以及各种特殊位置的块如行、列、角块等的访问。通过示例展示了如何进行块赋值和拷贝,以及对数组中的块进行操作。内容涵盖了矩阵的切片、复制和重组,是理解Eigen库高效使用的关键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

块 block

定义

Block of size (p,q), starting at (i,j)

  • 动态大小
matrix.block(i,j,p,q);
  • 固定大小
matrix.block<p,q>(i,j)

示例

右值

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

左值

#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 2x3 block:" << endl << a << endl << endl;
}

特殊的block: column 和row

  • 第i行 matrix.row(i)
  • j列: matrix.col(j)
#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 adding 3 times the first column into the third column, the matrix m is:\n";
  cout << m << endl;
}

特殊位置的块

Block operationVersion constructing a dynamic-size block expressionVersion constructing a fixed-size block expression
Top-left p by q blockmatrix.topLeftCorner(p,q);matrix.topLeftCorner<p,q>();
Bottom-left p by q blockmatrix.bottomLeftCorner(p,q);matrix.bottomLeftCorner<p,q>();
Top-right p by q blockmatrix.topRightCorner(p,q);matrix.topRightCorner<p,q>();
Bottom-right p by q blockmatrix.bottomRightCorner(p,q);matrix.bottomRightCorner<p,q>();
Block containing the first q rowsmatrix.topRows(q);matrix.topRows();
Block containing the last q rowsmatrix.bottomRows(q);matrix.bottomRows();
Block containing the first p columnsmatrix.leftCols§;matrix.leftCols

();

Block containing the last q columnsmatrix.rightCols(q);matrix.rightCols();
Block containing the q columns starting from imatrix.middleCols(i,q);matrix.middleCols(i);
Block containing the q rows starting from imatrix.middleRows(i,q);matrix.middleRows(i);
#include <Eigen/Dense>
#include <iostream>
 
using namespace std;
 
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;
}

数组中的块操作

Block operationVersion constructing a dynamic-size block expressionVersion constructing a fixed-size block expression
Block containing the first n elementsvector.head(n);vector.head();
Block containing the last n elementsvector.tail(n);vector.tail();
Block containing n elements, starting at position ivector.segment(i,n);vector.segment(i);
#include <Eigen/Dense>
#include <iostream>
 
using namespace std;
 
int main()
{
  Eigen::ArrayXf v(6);
  v << 1, 2, 3, 4, 5, 6;
  cout << "v.head(3) =" << endl << v.head(3) << endl << endl;
  cout << "v.tail<3>() = " << endl << v.tail<3>() << endl << endl;
  v.segment(1,4) *= 2;
  cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_console_

您的关注与鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值