Eigen Initialize Boolean Array
Asked Answered
A

4

5

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).

Arcadia answered 13/11, 2014 at 4:57 Comment(0)
I
9

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.

Ils answered 13/11, 2014 at 8:34 Comment(0)
A
2

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;
}
Aegisthus answered 13/11, 2014 at 6:1 Comment(0)
S
1

If I understand correctly what you're asking, then this scheme works

Eigen::Array<bool,1,5> v;
v << true, true, false, true, true;
Sannyasi answered 13/11, 2014 at 5:35 Comment(0)
E
0

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.
Euphonic answered 20/2 at 23:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.