【问题标题】:Get matrix views/blocks from a Eigen::VectorXd without copying (shared memory)从 Eigen::VectorXd 获取矩阵视图/块而不复制(共享内存)
【发布时间】:2014-02-28 16:28:49
【问题描述】:

有谁知道如何从 Eigen::VectorXf 中提取可以解释为特定 Eigen::MatrixXf 而不复制数据的块的好方法? (向量应该包含几个扁平矩阵)

例如类似的东西(伪代码):

VectorXd W = VectorXd::Zero(8);

// Use data from W and create a matrix view from first four elements
Block<2,2> A = W.blockFromIndex(0, 2, 2);
// Use data from W and create a matrix view from last four elements
Block<2,2> B = W.blockFromIndex(4, 2, 2);

// Should also change data in W
A(0,0) = 1.0
B(0,0) = 1.0

目的很简单,就是让多个表示指向内存中的相同数据。

这可以做到,例如在 python/numpy 中,通过提取子矩阵视图并重塑它们。

A = numpy.reshape(W[0:0 + 2 * 2], (2,2))

我不知道 Eigen 是否支持 Eigen::Block 的 reshape 方法。

我猜,Eigen::Map 非常相似,只是它需要普通的 c 数组/原始内存。 (链接:Eigen::Map)。

克里斯

【问题讨论】:

    标签: c++ eigen eigen3


    【解决方案1】:

    如果您想将子向量重新解释为矩阵,那么可以,您必须使用 Map:

    Map<Matrix2d> A(W.data());          // using the first 4 elements
    Map<Matrix2d> B(W.tail(4).data());  // using the last 4 elements
    Map<MatrixXd> C(W.data()+6, 2,2);   // using the 6th to 10th elements
                                        // with sizes defined at runtime.
    

    【讨论】:

    • 忘记了常用的data()方法:]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 2017-11-01
    • 2021-05-11
    • 2017-04-06
    • 2020-12-22
    • 2013-09-19
    相关资源
    最近更新 更多