[英]How to convert Eigen::eigenvector().col(0).real() to std vector?
I am working on a code in c++ that calulates the eigen vectors of a matrix using the Eigen library. 我正在使用C ++中的代码使用Eigen库计算矩阵的特征向量。 I need only the first column and only the real values of the eigen vector.
我只需要第一列,也只需要特征向量的实值。 Is there a way to copy these values to the
std::vector
data type? 有没有办法将这些值复制到
std::vector
数据类型? Can someone help me with this? 有人可以帮我弄这个吗?
I saw this Converting Eigen::MatrixXf to 2D std::vector post. 我看到了将Eigen :: MatrixXf转换为2D std :: vector后的内容。 But I need only the specific values.
但是我只需要特定的值。 Moreover, I am not sure what is the type that
eigenvector()
function returns. 而且,我不确定
eigenvector()
函数返回的类型是什么。 In the documentation, it is said as complex Eigen::Matrix
type. 在文档中,它被称为复杂的
Eigen::Matrix
类型。
This is an example code. 这是示例代码。
#include<iostream>
#include<Eigen/Eigenvalues>
#include<vector>
using namespace std;
struct eigen
{
float a, b, c;
};
int main()
{
vector<Eigen::Matrix3f> A(1);
A[0] << 1, 2, 3, 2, 4, 5, 3, 5, 6;
Eigen::EigenSolver<Eigen::Matrix3f> handle(A[0]);
cout << "The matrix of eigenvectors, V, is: " << endl << handle.eigenvectors() << endl << endl;
cout << "The real part of first column is : " << endl <<
handle.eigenvectors().col(0).real() << endl << endl;
return 0;
}
The output of the above code is 上面代码的输出是
The matrix of eigenvectors, V, is:
(0.327985,0) (-0.736977,0) (-0.591009,0)
(0.591009,0) (-0.327985,0) (0.736976,0)
(0.736976,0) (0.591009,0) (-0.327985,0)
The real part of the first column is :
0.327985
0.591009
0.736976
I need to copy the values of handle.eigenvectors().col(0).real()
to std::vector<eigen>
我需要将
handle.eigenvectors().col(0).real()
的值复制到std::vector<eigen>
地图就是答案:
Vector3f::Map(&v[0].a) = handle.eigenvectors().col(0).real();
You can add a constructor and/or an assignment operator to eigen
that copies the values. 您可以向复制值的
eigen
值添加构造函数和/或赋值运算符。
struct eigen
{
eigen() = default;
template <typename InputIterator, typename = std::iterator_traits<InputIterator>::value_type>
eigen(InputIterator first) : a(*first++), b(*first++), c(*first++) {}
template <typename InputIterator, typename = std::iterator_traits<InputIterator>::value_type>
eigen& operator=(InputIterator first)
{
a = *first++;
b = *first++;
c = *first++;
return *this;
}
float a, b, c;
};
int main()
{
Eigen::Matrix3f A;
A << 1, 2, 3, 2, 4, 5, 3, 5, 6;
Eigen::EigenSolver<Eigen::Matrix3f> handle(A);
std::cout << "The matrix of eigenvectors, V, is: " << std::endl << handle.eigenvectors() << std::endl << std::endl;
std::cout << "The real part of first column is : " << std::endl <<
handle.eigenvectors().col(0).real() << std::endl << std::endl;
eigen e = handle.eigenvectors().col(0).real();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.