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?
这对我有用:
A_2=A-B.asDiagonal();
The simplest and fastest way to do achieve this is:
Eigen::MatrixXd A1(3,3), B(3,1), A2;
...
A2 = A1;
A2.diagonal() -= B;
of course, better use the VectorXd
type for vectors (here for B
), and finally if B
is constant, then you can use array facilities:
A2.diagonal().array() -= 1;
for(int i = 0; i < matrix1.rows(); ++i)
matrix1(i, i) -= matrix2(i, 0);
This code iterates over each row of the matrix ( matrix1.rows()
) and subtracts the corresponding value of matrix2 ( matrix2(i, 0)
) from the diagonals in matrix1 ( matrix1(i, i)
).
Matrix manipulation in Eigen
works very similar to that of arrays. The indexing starts from zero and it is row major. The documentation ( Eigen: The Matrix Class is well written and may help you to solve future problems.
For your stated problem the solution is given below.
#include <iostream>
#include <eigen3/Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd matA(3,3), matB(3,1);
matA<<1,2,3,
4,5,6,
7,8,9;
matB<<1,1,1;
for(int i=0; i<3;i++)
matA(i,i) -= matB(i);
std::cout<<matA<<std::endl;
return 0;
}
However, I would use Matrix3d and Vector3d for the first and second matrix, respectively.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.