2

Let's say I have an Eigen matrix with many values that are close to zero:

M = 
[          0,           0,           0,           0]
[  0.0045549,   -0.048242,   0.0069165,  9.7007e-07]
[   0.048251,    0.004541,  -0.0046149,  1.8313e-06]

What is the most efficient way to write a function that takes this matrix and sets to 0 the values that are close to zero within a certain tolerance, e.g.:

setAlmostZeroToZero(M, 1e-4) = 
[          0,           0,           0,           0]
[  0.0045549,   -0.048242,   0.0069165,           0]
[   0.048251,    0.004541,  -0.0046149,           0]

mcamurri
  • 153
  • 11
  • `M.unaryExpr([](double x){return (abs(x)<1e-4)?0.:x;})` shouldn't be too bad, your compiler is likely to vectorize it. – Marc Glisse Nov 30 '19 at 18:43
  • Or something like `(M.abs()<1e-4).select(0.,M)` (untested) https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#a65e78cfcbc9852e6923bebff4323ddca – Marc Glisse Nov 30 '19 at 18:50
  • @MarcGlisse the second only works in the Array domain: https://godbolt.org/z/MbPBqV – chtz Dec 02 '19 at 09:02

0 Answers0