Skip to content

Instantly share code, notes, and snippets.

@dojeda
Created August 5, 2016 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dojeda/d0333a62b7959590184782fecc1d75a3 to your computer and use it in GitHub Desktop.
Save dojeda/d0333a62b7959590184782fecc1d75a3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <Eigen/Dense>
int main(int argc, char *argv[]) {
Eigen::MatrixXd X = Eigen::MatrixXd::Random(3, 3);
// PCA = EVD of the covariance matrix
Eigen::SelfAdjointEigenSolver < Eigen::MatrixXd > solver(X.selfadjointView<Eigen::Lower>());
Eigen::MatrixXd V = solver.eigenvectors(); // eigenvectors (in columns)
Eigen::VectorXd D = solver.eigenvalues();
std::cout << "X=\n" << X << std::endl;
std::cout << "V=\n" << V << std::endl;
std::cout << "D=\n" << D << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment