Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created April 23, 2018 17:28
Show Gist options
  • Save jefftrull/86b078527b124929b0de71161bd67689 to your computer and use it in GitHub Desktop.
Save jefftrull/86b078527b124929b0de71161bd67689 to your computer and use it in GitHub Desktop.
// std::vector<Eigen::VectorXf> -> Eigen::MatrixXf
#include <iostream>
#include <vector>
#include <Eigen/Dense>
int main() {
using namespace Eigen;
const int cols = 10;
const int rows = 10; // or do you get it from the sizes of each entry in *d?
std::vector<VectorXf> * d = new std::vector<VectorXf>(cols);
for (int col = 0; col < cols; ++col) {
(*d)[col] = VectorXf(rows);
(*d)[col].setRandom();
}
// now create the Matrix and copy from vector of vectors
Matrix<float, Dynamic, Dynamic> result(rows, cols); // assuming we know #rows...
for (int col = 0; col < cols; ++col) {
result.col(col) = (*d)[col];
}
IOFormat OctaveFmt(StreamPrecision, 0, ", ", ";\n", "", "", "[", "]");
std::cout << "result matrix=\n" << result.format(OctaveFmt) << "\n";
}
@JasonAlexander
Copy link

JasonAlexander commented Apr 23, 2018

// Ended up with this
    Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic > ev(d->size(), 128);
    for ( int r = 0; r < d->size(); ++r ) {
        for (int c = 0; c < 128; ++c) {
            ev(r,c) = (*d)[r](c);
        }
    }

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment