如何在Eigen中通过取n行/列然后跳过m行/列来对矩阵进行切片

  • 本文关键字:切片 然后 Eigen c++ eigen
  • 更新时间 :
  • 英文 :


我想问一个关于在Eigen中以有效方式进行切片的问题。我想以一种允许我取n行,然后跳过m行并重复该过程直到结束索引的方式对矩阵进行切片。例如,如果A是1000 x 20矩阵,我想形成矩阵B,它是800 x 20,它包含A的每个连续5行的前4行。

我希望我的问题是清楚的。

非常感谢,萨达姆·

取自特征文件

假设你的计算机上有这些库,你可以编译以下代码。测试如何将一列置于三列之上。

#include <eigen3/Eigen/Dense>
#include <iostream>

using namespace std;
using namespace Eigen;
int main()
{
MatrixXf M1 = MatrixXf::Random(3,8);
cout << "Column major input:" << endl << M1 << "n";
cout << M1.outerStride() << endl;
cout << (M1.cols()+2)/3 << endl;
Map<MatrixXf,0,OuterStride<> > M2(M1.data(), M1.rows(), (M1.cols()+2)/3, OuterStride<>(M1.outerStride()*3));
cout << "1 column over 3:" << endl << M2 << "n";

typedef Matrix<float,Dynamic,Dynamic,RowMajor> RowMajorMatrixXf;
RowMajorMatrixXf M3(M1);
cout << "Row major input:" << endl << M3 << "n";
Map<RowMajorMatrixXf,0,Stride<Dynamic,3> > M4(M3.data(), M3.rows(), (M3.cols()+2)/3,
Stride<Dynamic,3>(M3.outerStride(),3));
cout << "1 column over 3:" << endl << M4 << "n";
}

你可以试试这个,基本上我是从上一个向量创建一个子向量。我把栏目从20改为10,因为它们不变。

int rows = 1000;
int col = 10;
vector<int>arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<vector<int>>matrix;

for(int i = 0 ; i < rows ; i++)
{
matrix.push_back(arr);
}
int newrow = (rows / 5) * 4 ;
auto last = matrix.begin() + newrow;
vector<vector<int>>matrix2(matrix.begin(), last);
std::cout << matrix2.size() << "n";

相关内容

最新更新