[英]How to convert Eigen::eigenvector().col(0).real() to std vector?
我正在使用C ++中的代碼使用Eigen庫計算矩陣的特征向量。 我只需要第一列,也只需要特征向量的實值。 有沒有辦法將這些值復制到std::vector
數據類型? 有人可以幫我弄這個嗎?
我看到了將Eigen :: MatrixXf轉換為2D std :: vector后的內容。 但是我只需要特定的值。 而且,我不確定eigenvector()
函數返回的類型是什么。 在文檔中,它被稱為復雜的Eigen::Matrix
類型。
這是示例代碼。
#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 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
我需要將handle.eigenvectors().col(0).real()
的值復制到std::vector<eigen>
地圖就是答案:
Vector3f::Map(&v[0].a) = handle.eigenvectors().col(0).real();
您可以向復制值的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.