Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
602 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...