Constructing a diagonal matrix from vector of integers: function eigen

According to this part of the documentation you have quite a few options, the easiest one being

auto mat = vec.asDiagonal();

You should use proper types with Eigen, unless you really know what you're doing

//Create a 4x4 diagonal matrix from the vector [ 5 6 7 8 ]
Eigen::Vector4d vec;
vec << 5, 6, 7, 8;
Eigen::DiagonalMatrix<double, 4> mat = vec.asDiagonal();

Using auto is a really slippery slope where you typically have no idea what the compiler uses as the type, and coupled with Eigen, this is one of the common sources of tricky-to-find errors (see https://eigen.tuxfamily.org/dox/TopicPitfalls.html)

Tags:

C++

Eigen