Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

eigen

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?

like image 903
quant Avatar asked Jan 10 '23 20:01

quant


1 Answers

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]

like image 92
slajoie Avatar answered Jan 26 '23 00:01

slajoie