Reputation: 801
How do you initialize a boolean array in the Eigen library (C++) to a specific truth value? There are initializers for numeric matrices but I can't find an example for a boolean array (Eigen::Array).
Upvotes: 5
Views: 9847
Reputation: 71
Another way to do it..
Eigen::Array<bool, Eigen::Dynamic, 1 > x;
x = blahblah_etc; // Do some stuff that modifies x
// Want to re-initialize x
x = (x == x); // Set all x values to true, or..
x = (x != x); // Set all x values to false.
Upvotes: 0
Reputation: 29205
The other answer are correct, but for completeness let me add:
#include <Eigen/Dense>
using namespace Eigen;
typedef Array<bool,Dynamic,1> ArrayXb;
ArrayXb a = ArrayXb::Constant(5,true);
ArrayXb b(5);
b.setConstant(true); // no-resizing
b.fill(true); // alias for setConstant
b.setConstant(10,true); // resize and initialize
Array<bool, 5, 1> c(true);
In the last case, because here the size is known at compile time, the argument is interpreted as the initializing value.
Upvotes: 9
Reputation: 86208
If you are trying to initialize the entire array to either true
or false
what about something like this:
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
Array<bool, 1, 5> false_array(5);
false_array = Array<bool, 1, 5>::Zero(5);
Array<bool, 1, 5> true_array(5);
true_array = Array<bool, 1, 5>::Ones(5);
return 0;
}
Upvotes: 2
Reputation: 56557
If I understand correctly what you're asking, then this scheme works
Eigen::Array<bool,1,5> v;
v << true, true, false, true, true;
Upvotes: 1