10

This should hopefully be pretty simple but i cannot find a way to do it in the Eigen documentation.

Say i have a 2D vector, ie

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

Assume it is filled with 10 x 4 data set.

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

The obvious way is to use a for loop like this:

#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 there should be a better way that is native to Eigen?

4

1 回答 1

12

肯定的事。您不能一次完成整个矩阵,因为vector<vector>将单行存储在连续的内存中,但连续的行可能不连续。但是您不需要分配一行的所有元素:

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());
于 2013-09-17T00:10:48.173 回答