2

I would like to do the boolean matrix plus. How could I do it in Eigen? My following example only gives a scalar +.

#include "Eigen/Dense"
#include <iostream>

using namespace std;
using namespace Eigen;


int main()
{
   Eigen::Matrix<bool, 4, 4> m;
   m << 0,1,1,1,
    1,0,1,0,
    1,1,0,0,
    1,1,1,0;
   cout << m + m;  //should be logical_and here
}

How could I use the logical_and here?

wei wang
  • 155
  • 3

1 Answers1

1

Eigen does not seem to provide specific functions to work on boolean matrices. However you can use the fact that booleans are converted to 0 (false) and 1 (true) reliably (see bool to int conversion). Noting that 0=0*0=0*1=1*0 and 1*1=1 it is obvious that multiplication of the booleans as integers is the same (up to type) as logical and. Therefore the following should work:

#include "Eigen/Dense"
#include <iostream>

using namespace std;
using namespace Eigen;


int main()
{
   Eigen::Matrix<bool, 4, 4> m;
   m << 0,1,1,1,
    1,0,1,0,
    1,1,0,0,
    1,1,1,0;
   Eigen::Matrix<bool, 4, 4> result = m.cwiseProduct(m);
   cout << result;
}
Community
  • 1
  • 1