c – 用于矩阵向量cwiseProduct操作的Eigen3 replicate()

我有以下代码:

Eigen::MatrixXf aMatrix( 3, 5 );
aMatrix <<
1, 0, 1, 0, 1,
0, 1, 0, 1, 0,
1, 1, 1, 1, 1;

Eigen::VectorXf aVector( 5 );
aVector << 3, 4, 5, 6, 7;

cout << aMatrix.cwiseProduct( aVector.replicate( 1, aMatrix.rows() ).transpose() ) << endl;

哪个输出:

3 0 5 0 7
0 4 0 6 0
3 4 5 6 7

有没有比使用replicate()调用更有效的方法来实现这一点?

解决方法:

解决了(帮助来自:How can I apply bsxfun like functionality at Eigen?)

这些是等价的:

aMatrix.cwiseProduct( aVector.replicate( 1, aMatrix.rows() ).transpose() )
aMatrix.array().rowwise() * aVector.array().transpose()
上一篇:c – 复数矩阵乘法Eigen vs Matlab


下一篇:c – 用于连接矩阵的Eigen :: Ref