3

Say we have a matrix A of dimension MxN and a vector a of dimension Mx1. In Matlab, to multiply 'a' with all columns of 'A', we can do

bsxfun(@times, a, A)

Is there an equivalent approach in Eigen, without having to loop over the columns of the matrix?

I'm trying to do

M = bsxfun(@times, a, A) + bsxfun(@times, a2, A2)

and hoping that Eigen's lazy evaluation will make it more efficient.

Thanks!

bendervader
  • 2,619
  • 2
  • 19
  • 22

1 Answers1

5

You can do:

M = A.array().colwise()*a.array();

The .array() is needed to redefine the semantic of operator* to coefficient-wise products (not needed if A and a are Array<> objects).

In this special case, it is probably better to write it as a scaling operation:

M = a.asDiagonal() * A;

In both cases you won't get any temporary thanks to lazy evaluation.

ggael
  • 28,425
  • 2
  • 65
  • 71
  • What about C = bsxfun(@rdivide, pts, pts(3,:)); ? My related question: http://stackoverflow.com/questions/42741074/from-matlab-to-c-eigen-matrix-operations-vector-normalization?noredirect=1#comment72601063_42741074 – Pedro77 Mar 11 '17 at 23:26
  • This question has been answered by Peter. – ggael Mar 13 '17 at 10:59