指向eigen matrixxd的子行,表现得像vectorxd

pointer to sub-row of Eigen MatrixXd that behaves like a VectorXd

本文关键字:vectorxd eigen matrixxd 指向      更新时间:2023-10-16

我有一个eigen matrixxd,需要一个指针,以便某些行的某些后续条目。我希望能够使用这个指针。我有这样的东西:

Eigen::MatrixXd* matrix = new MatrixXd(3, 3);
(*matrix) <<    1, 2, 3,
                4, 5, 6,
                7, 8, 9;
Block<MatrixXd, 1, Dynamic, false, true> full_row = (*matrix).row(1);
// this gives me the full row. I am interested only in the row containing 5 6.
Block<MatrixXd> part_row = (*matrix).block(1, 1, 1, 2);
// this gives me the partial row that I want, but now i need two indices to 
// access an element. 
part_row(0, 1) = 3; // works
part_row(1) = 3; // gives compiler error

我希望能够直接访问部分行,而无需复制值。这确实很重要,因为它必须经常完成,而且我负担不起来回复制向量。(我相信我不能指望编译器优化复制,因为矩阵的大小通常未知)。任何帮助是极大的赞赏。欢呼!

您需要指定您的subbatrix是向量:

Block<MatrixXd,1,Dynamic> part_row(*matrix, 1, 1, 1, 2);