从Eigen :: VectorXd获取矩阵视图/块而不复制(共享内存)

时间:2014-02-04 15:48:37

标签: c++ eigen eigen3

有没有人知道如何从Eigen :: VectorXf中提取块,这可以解释为特定的Eigen :: MatrixXf而不复制数据? (向量应包含几个展平矩阵)

e.g。类似的东西(伪代码):

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的重塑方法。

我猜,Eigen :: Map非常相似,只是它需要普通的c-arrays / raw memory。 (链接:Eigen::Map)。

克里斯

1 个答案:

答案 0 :(得分:4)

如果要将子向量重新解释为矩阵,则是,您必须使用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.
相关问题