Initialize Eigen :: MatrixXd from 2d std :: vector

This will hopefully be fairly simple, but I cannot find a way to do this in the Eigen documentation.

Let's say I have a 2D vector, i.e.

std::vector<std::vector<double> > data

Suppose it is populated with a 10 x 4 dataset.

How can I use this data to populate an Eigen::MatrixXd mat .

The obvious way is to use a for loop as follows:

 #Pseudo code Eigen::MatrixXd mat(10, 4); for i : 1 -> 10 mat(i, 0) = data[i][0]; mat(i, 1) = data[i][1]; ... end 

But should there be a better way that is native to Eigen?

+8
c ++ eigen
source share
1 answer

Of course. You cannot execute the whole matrix at once, because vector<vector> stores single lines in adjacent memory, but consecutive lines may not be adjacent. But you do not need to assign all the elements of the string:

 std::vector<std::vector<double> > data; MatrixXd mat(10, 4); for (int i = 0; i < 10; i++) mat.row(i) = VectorXd::Map(&data[i][0],data[i].size()); 
+9
source share

All Articles