Convert vector<vector<double> > to Eigen Matrix?

Viewed 613

Suppose I have a C++ vector of vector of double, vector<vector<double> >. This variable would typically represent the contents of a CSV file, where each row in the file is a sequence of double values. Each row has the same count of items.

What is the most elegant way to convert this vector<vector<double> > into a 2-D Eigen MatrixXd?

I was hoping for something like below, where I would like to create a 3x5 matrix from the vector of vector of double.

#include "Eigen/Dense"
#include <vector>

using Eigen::MatrixXd;

int main(int argc, char**argv) {

    std::vector<double> row1 = {  1.0,  2.0,  3.0,  4.0,  5.0 };
    std::vector<double> row2 = { 11.0, 12.0, 13.0, 14.0, 15.0 };
    std::vector<double> row3 = { 21.0, 22.0, 23.0, 24.0, 25.0 };

    std::vector<std::vector<double> > vv;
    vv.push_back(row1);
    vv.push_back(row2);
    vv.push_back(row3);

    MatrixXd mymatrix(vv); // NOPE! Compiler error.

    return 0;
}

Compiling result:

% g++ -std=c++11 test_eigen.cpp

test_eigen.cpp:85:14: error: no matching constructor for initialization of 'MatrixXd' (aka
      'Matrix<double, Dynamic, Dynamic>')
    MatrixXd matrix(vv); // NOPE!
             ^      ~~

Note that in Python, converting from a list of list to a Numpy matrix exhibits the simplicity I'm looking for:

import numpy as np

row1 = [  1.0,  2.0,  3.0,  4.0,  5.0 ]
row2 = [ 11.0, 12.0, 13.0, 14.0, 15.0 ]
row3 = [ 21.0, 22.0, 23.0, 24.0, 25.0 ]
list_of_list = [row1, row2, row3];

mymatrix = np.array(list_of_list)

Related question but with 1-D vectors:

Initialise Eigen::vector with std::vector

1 Answers

Thanks to user20650, I found a duplicate question. I generalized its solution into a reusable function:

// Convert a 2-D vector<vector<double> > into an Eigen MatrixXd.
// Throws exception if rows do not have same length.
MatrixXd convert_vvd_to_matrix(vector<vector<double> > vvd) {

    std::size_t n_rows = vvd.size();
    std::size_t n_cols = vvd.at(0).size();

    MatrixXd result(n_rows, n_cols);
    result.row(0) = VectorXd::Map(&vvd[0][0], n_cols);

    // Add each vector row to the MatrixXd. 
    for (std::size_t i = 1; i < n_rows; i++) {

        // Make sure that every row of vvd has the same size.
        if (n_cols != vvd.at(i).size()) {
            char buffer[200];
            snprintf(buffer, 200, 
                        "vvd[%ld] size (%ld) does not match vvd[0] size (%ld)",
                        i, vvd.at(i).size(), n_cols);
            string err_mesg(buffer);
            throw std::invalid_argument(err_mesg);
        }

        result.row(i) = VectorXd::Map(&vvd[i][0], n_cols);
    }

    return result;
}

Example usage:

#include <cstdio>
#include <iostream>
#include "Eigen/Dense"
#include <vector>

using std::cout;
using std::endl;
using std::vector;
using Eigen::MatrixXd;

void test_vector_vector_double() {

    vector<double> row1 = {  1.0,  2.0,  3.0,  4.0,  5.0 };
    vector<double> row2 = { 11.0, 12.0, 13.0, 14.0, 15.0 };
    vector<double> row3 = { 21.0, 22.0, 23.0, 24.0, 25.0 };

    vector < vector<double> > vvd;
    vvd.push_back(row1);
    vvd.push_back(row2);
    vvd.push_back(row3);

    MatrixXd mat = convert_vvd_to_matrix(vvd);

    cout << mat << endl;
}

Output:

 1  2  3  4  5
11 12 13 14 15
21 22 23 24 25
Related