Before anything else I must say that I've studied Comparing two matrices with eigen but my question is not the same. Suppose I have two eigen matrices A and B, and I want to edit A in following way:
if (A(i,j) > B(i,j)) A(i,j) = A(i,j) otherwise A(i,j) = B(i,j)
I guess it is possible to do it without an explicit for loop. But I am not very proficient with Eigen yet. What whould be the best approach?
It's A.cwiseMax(B)
.
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::Matrix2i A = Eigen::Matrix2i::Random();
Eigen::Matrix2i B = Eigen::Matrix2i::Random();
std::cout << "A =\n" << A << "\nB =\n" << B << "\n";
A = A.cwiseMax(B);
std::cout << "max(A,B) =\n" << A << "\n";
}
Output on my machine is
A =
730547559 607950953
-226810938 640895091
B =
884005969 -353856438
-649503489 576018668
max(A,B) =
884005969 607950953
-226810938 640895091