I've two Eigen VectorXi, containining 0 and 1.
VectorXi X;
X << 0, 1,1,1,1,0,0,1,0;
VectorXi Y;
Y << 1, 0,1,1,0,0,1,1,0;
I want to get the elementwise boolean AND and OR to obtain the number of elements in the union and in the intersection of the two arrays.
In Matlab this corresponds to:
x = [0,1,1,1,1,0,0,1,0];
y = [1,0,1,1,0,0,1,1,0];
sum(x & y)
sum(x | y)
But I don't understand how to do it without for loops in Eigen. Something like:
X.array() | Y.array()
does not work.
Some ideas?
Why are only normal operations implemented as cwise
operations?
For this particular case, you can use
X.cwiseProduct(Y)
(X + Y).cwiseMin(1)
This depends on the elements being only 0 and 1. More generally, you can define custom binary functors:
struct BinaryOr {
typedef int result_type;
int operator()(int a, int b) const { return a | b; }
};
struct BinaryAnd {
typedef int result_type;
int operator()(int a, int b) const { return a & b; }
};
and use
X.binaryExpr(Y, BinaryOr())
X.binaryExpr(Y, BinaryAnd())