Mihai Bujanca
Mihai Bujanca

Reputation: 4209

Access eigenvalues

I am trying to get the smallest the eigenvalues and eigenvectors of a covariance matrix:

    Eigen::Matrix3d covariance_matrix; //has to be Matrix3d
    double minEigenValue = 0;
    int minEigenVectorIndex = 0;
    //compute covariance matrix
    Eigen::EigenSolver<Eigen::Matrix3d > solver(covariance_matrix);
    Eigen::Matrix eigenvalues = solver.eigenvalues();
    // Eigen::Matrix3d eigenvalues = solver.eigenvalues(); results in an error
    for(int i = 0; i < 3;i++)
    {
//How do I access the eigenvalues? This fails. eigenvalues[0][i] also fails
        if(eigenvalues(0,i) > minEigenValue) 
        {
            minEigenValue = eigenvalues(0,i);
            minEigenVectorIndex = i;
        }
    }
    // somehow get pair of vector[0], vector[1], vector[2]: 
   //solver.eigenvectors().col(minEigenVectorIndex);

I have read through the documentation quite a lot, but couldn't find a clear example / explanation How do I access the eigenvectors and values?

Upvotes: 1

Views: 399

Answers (1)

Adrian Roman
Adrian Roman

Reputation: 531

Eigen::Matrix<std::complex<double>,3,1> eigenvalues = solver.eigenvalues(); Eigen::Matrix<std::complex<double>,3,3> eigenvec = solver.eigenvectors();

Upvotes: 2

Related Questions