This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Clipping an Eigen array

Tags: None
(comma "," separated)
GeorgeKnight
Registered Member
Posts
15
Karma
0

Clipping an Eigen array

Wed Sep 04, 2013 2:39 pm
Hi,

Is there an efficient way to "clip" an Eigen array? I.e. to set all values above a given threshold to this threshold?
jitseniesen
Registered Member
Posts
204
Karma
2

Re: Clipping an Eigen array

Wed Sep 04, 2013 2:48 pm
a = a.max(0.5); // threshold is 0.5, a is an array; use .cwiseMax() instead of .max() for matrices
GeorgeKnight
Registered Member
Posts
15
Karma
0

Re: Clipping an Eigen array

Wed Sep 04, 2013 3:32 pm
jitseniesen wrote:a = a.max(0.5); // threshold is 0.5, a is an array; use .cwiseMax() instead of .max() for matrices

Thanks for the quick reply! When I was scrolling through the docs I saw that function, but I was erroneously dismissing it, thinking it was the maximum value of the Matrix.

For the problem I described above, I would actually need

a = a.min(0.5);

Before your answer, I was trying to cook up an alternative which is not nearly as elegant as min(), but which illustrates the use of unaryExpr() with lambda functions, so I'll give it anyway in case it might be useful for someone else.

Code: Select all
#include <iostream>
#include <cstdlib>
#include <Eigen/Dense>

int main()
{
    Eigen::ArrayXXd array(3,3);
    array << 1.0, 2.0, 3.0,
             4.0, 8.0, 5.0,
             8.0, 8.0, 2.0;

    double threshold = 6.0;
    array = array.unaryExpr([threshold](double x) -> double {return (x > threshold) ? threshold : x;});

    std::cout << array << std::endl;
    return EXIT_SUCCESS;
}


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]