Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen3 replicate() for a matrix-vector cwiseProduct operation

I have the following code:

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;

which outputs:

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

Is there a more efficient way to achieve this than using the replicate() call?

like image 746
pt3dNyc Avatar asked Mar 19 '23 15:03

pt3dNyc


1 Answers

Solved (with help from: How can I apply bsxfun like functionality at Eigen?)

These are equivalent:

aMatrix.cwiseProduct( aVector.replicate( 1, aMatrix.rows() ).transpose() )
aMatrix.array().rowwise() * aVector.array().transpose()
like image 179
pt3dNyc Avatar answered Mar 27 '23 00:03

pt3dNyc