Suppose I have an array filled with doubles:
Eigen::Array<double,m,n> myarray;
Now I want to replace any elements of myarray
which are not finite with the number 0.0
How would I do this?
I was thinking of multiplying it by an array of values with zeros where I find infinity, like this:
myarray *= myarray.cwiseEqual(std::numeric_limits<double>::infinity()) == 0.0;
And doing this for every invalid type. But this is really messy. Is there a better way?
Here's one easy way to do it:
myarray = myarray.unaryExpr([](double v) { return std::isfinite(v)? v : 0.0; });
Source: [http://eigen.tuxfamily.org/dox/classEigen_1_1ArrayBase.html#a23fc4bf97168dee2516f85edcfd4cfe7]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With