矩阵向量 cwiseProduct 操作的 Eigen3 replicate()

pt3*_*Nyc 4 c++ matrix-multiplication eigen eigen3

我有以下代码:

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;
Run Code Online (Sandbox Code Playgroud)

输出:

3 0 5 0 7
0 4 0 6 0
3 4 5 6 7
Run Code Online (Sandbox Code Playgroud)

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

pt3*_*Nyc 5

已解决(在以下方面的帮助下:如何在 Eigen 中应用类似 bsxfun 的功能?)

这些是等效的:

aMatrix.cwiseProduct( aVector.replicate( 1, aMatrix.rows() ).transpose() )
aMatrix.array().rowwise() * aVector.array().transpose()
Run Code Online (Sandbox Code Playgroud)