Search code examples
c++matrixeigendiagonal

Modify Eigen matrix diagonal


I have an Eigen::MatrixXd and I need to modify the value of the elements in its diagonal. In particular I have another Eigen::MatrixXd with one single column and the same number of rows of the first matrix.

I need to subtract to the diagonal of the first matrix the value of the elements of the second matrix.

Example:

A
 1 2 3
 4 5 6
 7 8 9

B
 1
 1
 1


A'
 0 2 3
 4 4 6
 7 8 8

How can I do?


Solution

  • This works for me:

    A_2=A-B.asDiagonal();