How do I find and replace all non-finite numbers in an Eigen::Array object?
Asked Answered
F

1

5

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?

Foggy answered 8/4, 2014 at 4:6 Comment(0)
A
12

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]

Argile answered 5/7, 2014 at 21:12 Comment(1)
oh, minCoeff() returns NAN only on debug. Odd Eigen behavior.Hoffer

© 2022 - 2024 — McMap. All rights reserved.