Question

I have to matrices and would like to treat them as a 1-D list and do a dot product. I the following, but it is not working:

Eigen::MatrixXf a(9,9), b(9,9);
float r = a.array().dot(b.array());

What would be the best way to do it?

Was it helpful?

Solution

Computing the coefficient-wise product of 2 matrices is a common pattern, so Eigen provides the cwiseProduct() method to write it elegantly. This would lead to the following expression:

float r = a.cwiseProduct(b).sum();

OTHER TIPS

Try this. :)

Eigen::MatrixXf a(9, 9), b(9, 9);

Eigen::Map<Eigen::VectorXf> aVector(a.data(), 81);
Eigen::Map<Eigen::VectorXf> bVector(b.data(), 81);

float squareError = aVector.dot(bVector);

Here is documentation about Map.

Actually I found it out:

float r = (a.array()*b.array()).sum();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top