Skip to content

Instantly share code, notes, and snippets.

@sat0b
Created June 21, 2017 22:45
Show Gist options
  • Select an option

  • Save sat0b/d3384d5311f050305a7be22aadf8a7e4 to your computer and use it in GitHub Desktop.

Select an option

Save sat0b/d3384d5311f050305a7be22aadf8a7e4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <Eigen/Core>
using namespace std;
using namespace Eigen;
int main() {
Matrix4d d4;
cout << "d4" << endl;
cout << d4 << endl;
Matrix<double, 5, 5> d5;
cout << "d5" << endl;
cout << d5 << endl;
Matrix<double, 2, 3> d23;
cout << "d23" << endl;
cout << d23 << endl;
Matrix<double, Dynamic, Dynamic> d43 = MatrixXd(4, 3);
cout << "4, 3" << endl;
cout << MatrixXd(4, 3) << endl;
cout << "zero(3, 3)" << endl;
cout << MatrixXd::Zero(3, 3) << endl;
// std::vector to Eigen::√ector
vector<double> r = {1, 2, 3};
VectorXd v3 = Map<VectorXd>(&r[0], r.size());
cout << "Mapping vector" << endl;
cout << v3 << endl;
// std::vector to Eigen::Matrix
vector<double> vec(16);
MatrixXd A = Map<MatrixXd>(vec.data(), 4, 4);
cout << "\nA" << endl;
cout << A << endl;
// std::vector<vector<double>> to Eigen::Matrix
vector<vector<double>> mat {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
const int N = mat.size();
const int M = mat[0].size();
vector<double> vmat(N*M);
for (int i = 0; i < N; ++i) {
std::copy(mat[i].begin(), mat[i].end(), vmat.begin() + i * M);
}
MatrixXd B = Map<Matrix<double, Dynamic, Dynamic, RowMajor>>(vmat.data(), N, M);
cout << "\nB" << endl;
cout << B << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment