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

Cwise product between a matrix and a vector (broadcast)?

Tags: None
(comma "," separated)
derkuci
Registered Member
Posts
2
Karma
0
OS
If A is a MatrixXd of size 3x4 and b is a VectorXd of length 4. How can I multiple A and b, element-wise, with b broadcast to every row of A? Something like A.rowwise().cwiseProduct(b) but apparently this doesn't compile.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You are looking for a diagonal product:

A * b.asDiagonal()

An alternative is:

a.array().rowwise() * b.array()
derkuci
Registered Member
Posts
2
Karma
0
OS
Thanks for the suggestions.

For the first one, would there be performance penalty than simply looping over A's rows (e.g., for (int i = 0; i < A.rows(); ++i) A.row(i).array() *= b.array();)?

For the 2nd one, I got compiling errors: no match for ‘operator*’ in ... Eigen::DenseBase<Derived>::rowwise [...]() * Eigen::MatrixBase<Derived>::array() [...]()’



ggael wrote:You are looking for a diagonal product:

A * b.asDiagonal()

An alternative is:

a.array().rowwise() * b.array()
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Diagonal products are fully optimized and vectorized. There is no overhead.

The second version is indeed for the devel version only.


Bookmarks



Who is online

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