1 #include <Eigen/Core>
2 #include <iostream>
3 using namespace Eigen;
4 using namespace std;
5 
6 // define a custom template unary functor
7 template<typename Scalar>
8 struct CwiseClampOp {
CwiseClampOpCwiseClampOp9   CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {}
operator ()CwiseClampOp10   const Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup ? m_sup : x); }
11   Scalar m_inf, m_sup;
12 };
13 
main(int,char **)14 int main(int, char**)
15 {
16   Matrix4d m1 = Matrix4d::Random();
17   cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << endl;
18   return 0;
19 }
20