Eigen: mask an array
Asked Answered
C

2

12

Is it possible to mask an array in Eigen as in Matlab?

Something like

ArrayXd arrayA = ArrayXd::Random(10, 5);
ArrayXi mask = ArrayXi::Zero(arrayA.rows(), arrayA.cols());
mask = arrayA > 5;
ArrayXd arrayB = arrayA(mask)

where arrayB is a row vector containing all and only the elements of arrayA>5

I could find similar requests but not any updated answer after 2011 ( https://forum.kde.org/viewtopic.php?f=74&t=98382 , https://forum.kde.org/viewtopic.php?f=74&t=98093 , https://forum.kde.org/viewtopic.php?f=74&t=97652)

Cp answered 9/7, 2014 at 13:19 Comment(1)
It's a decade later... But I think the code you wrote just works now. SeeDittmer
E
11

From the Quick Reference

(R.array() < s).select(P,Q);  // (R < s ? P : Q)

so, in your case it would be

(arrayA > 5).select(mask, arrayA)
Ecospecies answered 10/7, 2014 at 16:46 Comment(2)
But the result will be full of 0 , so it's the same result that you get with (arrayA > 5).cast<double>()Cp
Anyway good to know for the equivalent of matlab B(A> 5) = A(A> 5)Cp
C
2

I found some reference here http://igl.ethz.ch/projects/libigl/matlab-to-eigen.html

For B = IM(A), they suggest:

B = A.unaryExpr(bind1st(mem_fun( 
    static_cast<VectorXi::Scalar&(VectorXi::*)(VectorXi::Index)>
    (&VectorXi::operator())), &IM)).eval();

But it's not a generalized solution (and actually I couldn't try it )

Cp answered 10/7, 2014 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.