0

I have a vector of size N that contains vectors of size 3 (3D points). How can I convert this to an eigen matrix with N rows and 3 columns?

anonymous
  • 136
  • 3

1 Answers1

-1

This should get the job done, if I understood your question.


#include <Eigen/Geometry>
#include <Eigen/Dense>
#include <iostream>
#include <vector>

Eigen::MatrixXf convert_vect2mat(std::vector<std::vector<float> > & input_vector)
{
    
    int n_row = input_vector.size();
    int n_col = input_vector[0].size();       // based on your description this should be 3.
    Eigen::MatrixXf output_matrix(n_row, n_col);

    for(int i = 0; i < n_row; i++)
    {
        output_matrix(i, 0) = input_vector[i][0];
        output_matrix(i, 1) = input_vector[i][1];
        output_matrix(i, 2) = input_vector[i][2];
    }
    //std::cout << output_matrix << std::endl;
    return output_matrix;
}

int main()
{
    std::vector<std::vector<float> > vect = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};
    Eigen::MatrixXf result = convert_vect2mat(vect);
    std::cout << result << std::endl;
    return 0;
}
csg
  • 8,096
  • 3
  • 14
  • 38
  • 1
    This is probably (and hopefully) not what anybody would want: It just copies all elements and is thus not very helpful. You also include a lot of headers which are not needed. See https://stackoverflow.com/questions/33668485/eigen-and-stdvector for a better answer. – dtell Oct 25 '20 at 20:00