This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Coefficient-Wise Product and Broadcasting

Tags: None
(comma "," separated)
BigDaddyDrew
Registered Member
Posts
14
Karma
0
Relatively new Eigen user, but I've found it to be great so far!

I have a matrix and a row vector. I'm trying to multiply the vector coefficient-wise into each of the matrix's rows. Basically the following:

Code: Select all
myMatrix.rowwise().cwiseProduct( myRowVector );


This gives a compile error since the return type of rowwise() evidently isn't a matrix (or something that implements the cwiseProduct() method). So, I needed to loop over the rows to do what I wanted. Is there a better way?

Code: Select all
for ( unsigned int i( 0 ); i < myMatrix.rows(); ++i )
  myMatrix.row( i ) = myMatrix.row( i ).cwiseProduct( myRowVector );


Thanks!
BigDaddyDrew
Registered Member
Posts
14
Karma
0
I did some tests, and using replicate() on the row vector was actually a tad faster than the looping mechanism from before. So, I have a more efficient solution, and am down to a sexy one-liner. =)

Code: Select all
myMatrix.cwiseProduct( myRowVector.replicate( myMatrix.rows(), 1 ) );
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
with default branch (soon 3.1) you can do: mat.array().rowwise() * rowvec;
BigDaddyDrew
Registered Member
Posts
14
Karma
0
Beautiful, thanks!

I guess the other (easier) workaround for the time being is to multiply by a diagonal like so:

Code: Select all
myMatrix * myRowVector.asDiagonal();
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
right, the diagonal product is the answer I used to give for this recurrent question, I don't know why I forgot it this one. Note that diagonal products are properly optimized.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]