繁体   English   中英

从两个原始向量创建一个“ Eigen :: Matrix”

[英]Creating an `Eigen::Matrix` from two raw vectors

有没有一种创建Eigen :: MatrixXd的方法,其中每列都来自不同的“原始”向量。 例如,我想从文档中获取以下代码:

int array[8];
for(int i = 0; i < 8; ++i) array[i] = i;
cout << "Column-major:\n" << Map<Matrix<int,2,4> >(array) << endl;

并将其更改为

int array1[4];
int array2[4];
for(int i = 0; i < 4; ++i) array1[i] = i;
for(int i = 4; i < 8; ++i) array2[i-4] = i;
Eigen::Map<Eigen::MatrixXd> m(nullptr, 4, 2);
m.col(0) = array1;
m.col(1) = array2;
cout << "Column-major:\n" << m << endl;

或类似的东西...有没有办法做到这一点?

如果只有两个数组,则可以大步跨步:

Map<MatrixXd,0,OuterStride<> > m(array1, 4, 2, OuterStride<>(array2-array1));

否则,您可以按照NullaryExr的示例轻松地用10行代码编写内容。 基本上,您只需要一个类:

class my_mat_type_func {
  std::vector<double*> m_column_pointers;
  Eigen::Index rows;
public:
  const double& operator() (Index row, Index col) const {
    return m_column_pointers[col][row];
  }
  // custom API to fill data, resize, etc.
};

并将其包装为NullaryExpr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM