VectorXd asDiagonal Creates a diagonal matrix from a vector.
The asDiagonal function is a member function of the Eigen::VectorXd class in the Eigen C++ library. This function creates a diagonal matrix from a vector by placing the vector elements along the diagonal of the matrix.
Example 1:
Eigen::VectorXd v(3); v << 1, 2, 3; Eigen::MatrixXd m = v.asDiagonal(); std::cout << "Matrix m:\n" << m << std::endl;
The output of this code snippet will be:
Matrix m: 1 0 0 0 2 0 0 0 3
Here, a vector v of size 3 is created and initialized with values {1, 2, 3}. Then, a 3x3 matrix m is created using the asDiagonal function of v. The resulting matrix m will have the vector elements on its diagonal.
Example 2:
Eigen::VectorXd v(4); v << 4, 6, 2, 8; Eigen::MatrixXd m = v.asDiagonal(); std::cout << "Matrix m:\n" << m << std::endl;
The output of this code snippet will be:
Matrix m: 4 0 0 0 0 6 0 0 0 0 2 0 0 0 0 8
Here, a vector v of size 4 is created and initialized with values {4, 6, 2, 8}. Then, a 4x4 matrix m is created using the asDiagonal function of v. The resulting matrix m will have the vector elements on its diagonal.
The asDiagonal function is a member function of Eigen::VectorXd, which is part of the Eigen C++ library.
C++ (Cpp) VectorXd::asDiagonal - 21 examples found. These are the top rated real world C++ (Cpp) examples of VectorXd::asDiagonal extracted from open source projects. You can rate examples to help us improve the quality of examples.