user1612986 user1612986 - 1 month ago 10
C++ Question

Eigen Matrix library complement of a block operation

is there a compact/nice/efficient way to perform a "complement of a block" operation. Meaning say I want to extract a block of 10 rows from a matrix M starting from row 3 we can do something like:

Eigen::MatrixXd myBlock = M.block(3,0,10,M.cols());


but what I am trying to extract is "all rows but those 10 rows". That is from row 0 to 2 and then from rows 13 to the end. Is this possible ? Thanks in advance.

Answer

It is not possible directly, but you can easily concatenate two blocks together:

// result must be allocated to the correct size before concatenating:
Eigen::MatrixXd myComplement(M.rows()-10, M.cols());
myComplement << M.topRows<3>(), M.bottomRows(M.rows()-13);

N.B.: Instead of M.block(3,0,10,M.cols()) you can write M.middleRows<3>(10).