【问题标题】:Eigen::MatrixXd.block assignment using a std::vector使用 std::vector 的 Eigen::MatrixXd.block 分配
【发布时间】:2019-06-21 11:21:18
【问题描述】:

想法是将 std::vector 中的值分配给 Eigen::MatrixXd 中的块。

我的问题是,有什么好的方法吗?

 1 1 1        1   1   1
 1 1 1    to  1 102 123
 1 1 1        1   1   1

我尝试将 std::vector 转换为 Eigen::Map,但没有成功。 我有一个工作代码 [跟随 sn-p],但它并没有多大吸引力。也许有更简单的方法?

    void do_work(Eigen::MatrixXd &m, const std::vector<double> &v,
                 const Index i, const Index j, 
                 const Index p, const Index q) {
      auto stop = j + q;
      for (Index start = j, idx = 0; start < stop; ++start, ++idx)
        m.block(i, start, p, 1) << v[idx];
    }

    Eigen::MatrixXd m(3, 3);
    m << 1, 1, 1, 1, 1, 1, 1, 1, 1;
    std::vector<double> v = {102, 123};
    Index change_row = 0;
    Index change_column_from = 1, change_column_to = v.size();
    do_work(m, v, change_row, change_column_from, 1, change_column_to);

预期的结果是以高效(也许更干净)的方式执行操作。

【问题讨论】:

    标签: c++ eigen stdvector


    【解决方案1】:

    std::vector&lt;double&gt; 转换为Eigen::Map 写入,例如

    Eigen::Map<Eigen::VectorXd> map(v.data(), v.size());
    

    对于可读写访问,或以下,如果您对您的向量具有只读访问权限:

    Eigen::Map<const Eigen::VectorXd> map(v.data(), v.size());
    

    这可以分配给一个块表达式,假设块的大小与地图的大小匹配:

    m.block(i,j, map.size(), 1) = map.transpose();
                                   // transpose is actually optional here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-06
      • 2022-01-22
      • 2019-07-08
      • 1970-01-01
      • 2012-11-23
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多