Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if any element of Eigen::Matrix is different from zero

I have an Eigen::Matrix<double, Dynamic, Dynamic>, and I need to check if any of its elements is different from 0.

I tried the following code:

Matrix<double, Dynamic, Dynamic> m;
bool f = (m != 0.0).any();

But I got a compiler error.

Invalid operands to binary expression ('const Eigen::Matrix' and 'double')

like image 722
Nick Avatar asked Dec 02 '15 15:12

Nick


1 Answers

In Eigen, most of the element-wise operations are handled by an Array class. Fortunately, there is a simple way to use them on Matrix objects. Try

bool f = (m.array() != 0.0).any();
like image 122
Avi Ginsburg Avatar answered Oct 05 '22 22:10

Avi Ginsburg