Question

I have an Eigen MatrixXd and need a pointer to some subsequent entries of some row. I would like to be able to use this pointer. I have something like this:

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

I would like to be able to directly access the partial row, without having to copy the values. This is really important, since it has to be done often and I cannot afford to copy vectors back and forth. (I believe I cannot expect the compiler to optimize out the copying, since the sizes of the matrices are generally unknown). Any help is greatly appreciated. Cheers!

Was it helpful?

Solution

You need to specify that your submatrix is a vector:

Block<MatrixXd,1,Dynamic> part_row(*matrix, 1, 1, 1, 2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top