00001 #include <Eigen/Core> 00002 #include <iostream> 00003 using namespace Eigen; 00004 using namespace std; 00005 00006 // define a custom template unary functor 00007 template<typename Scalar> 00008 struct CwiseClampOp { 00009 CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {} 00010 const Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup ? m_sup : x); } 00011 Scalar m_inf, m_sup; 00012 }; 00013 00014 int main(int, char**) 00015 { 00016 Matrix4d m1 = Matrix4d::Random(); 00017 cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << endl; 00018 return 0; 00019 }