ejang
ejang

Reputation: 4062

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;

Upvotes: 11

Views: 11826

Answers (2)

user674155
user674155

Reputation:

As pointed out in the answer to an similar question here: Submatrices and indices using Eigen, libigl adds this functionality to Eigen.

igl::slice(A,indices,B);

Is equivalent to

B = A(indices)

Upvotes: 0

Amro
Amro

Reputation: 124563

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;
}

Upvotes: 16

Related Questions