4

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

stackoverflowuser2010
  • 38,621
  • 48
  • 169
  • 217
  • You are missing ""using namespace Eigen;"" or change your line MatrixXd mymatrix(vv); to eigen::MatrixXd mymatrix(vv); C++ requires the usage of namespaces – Jhovanny Uribe Sep 13 '21 at 01:54
  • Yes, I have `using Eigen::MatrixXd;` but forgot to include it in my post. I have updated my question. – stackoverflowuser2010 Sep 13 '21 at 02:12
  • Why not just write a function to create a dynamic Eigen matrix. Takes less time than asking the question. Might be a good idea to check that all the sizes of the vector rows are the same too. – doug Sep 13 '21 at 04:06
  • @doug: I was hoping Eigen would be have an elegant solution like Numpy. I also don't know what a "dynamic" Eigen matrix is. – stackoverflowuser2010 Sep 13 '21 at 06:27
  • probably a duplicate of https://stackoverflow.com/questions/18839240/initialize-an-eigenmatrixxd-from-a-2d-stdvector/18839353#18839353 – user20650 Sep 13 '21 at 23:36
  • 1
    @user20650: Thank you for the pointer to the other question.. – stackoverflowuser2010 Sep 14 '21 at 01:35

1 Answers1

2

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
stackoverflowuser2010
  • 38,621
  • 48
  • 169
  • 217