Although I would use the Jitse method to give you one more choice, here is a solution that is a bit more verbose and might seem better where the parts go:
Eigen::MatrixXd newC(C.rows()+1, C.cols()+1); newC.topRows<1>() = v.transpose(); newC.leftCols<1>() = v; newC.bottomRightCorner(C.rows(),C.cols()) = C; C.swap(newC)
Note that this assigns the upper left element twice; if you don't want this, replace the third line with this ugly one:
newC.topRows<1>().tail(v.size()-1) = v.transpose().tail(v.size()-1);
As a final note about why we do not use resize : it always discards the contents of your matrix (even when the matrix is โโcompressed), unless the number of elements remains the same, that is, when you resize MxN size of the matrix (M*k)x(N/k) .
source share