Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen boolean array slicing

In MATLAB it is common to slice out values that satisfy some condition from a matrix/array (called logical indexing).

vec = [1 2 3 4 5];
condition = vec > 3;
vec(condition) = 3;

How do I do this in Eigen? So far I have:

Eigen::Matrix<bool, 1, 5> condition = vec.array() > 3;
like image 527
ejang Avatar asked Apr 27 '13 14:04

ejang


1 Answers

Try this:

#include <iostream>
#include <Eigen/Dense>

int main()
{
    Eigen::MatrixXi m(1, 5);
    m << 1, 2, 3, 4, 5;
    m = (m.array() > 3).select(3, m);
    std::cout << m << std::endl;

    return 0;
}
like image 183
Amro Avatar answered Sep 27 '22 21:09

Amro