poster
2019-11-27, 07:35
<p>I've been looking all over for a more "eigen" way of implementing the functionality of Matlab's Logical indexing. Here's the best I could come up with. (Focusing on an int array here for simplicity)</p>
<pre><code>//an attempt at matlab-style Logical Indexing
//equivalent to the matlab:
// original = [1,2,3,4]
// subset = original(original < 3)
using namespace Eigen;
using std::cout;
using std::endl;
IOFormat OctaveFmt(StreamPrecision, 0, ", ", " ", "", "", "[", "]");
ArrayXi original(4);
original << 1,2,3,4;
cout<<"Original with bad values:"<<endl
<<original.format(OctaveFmt)<<endl;
Array<bool, Dynamic,1> selections = original < 3;
cout<<"One if it's a good value:"<<endl
<<selections.format(OctaveFmt)<<endl;
std::vector<int> picked;
for(int i = 0; i < selections.size(); i++ )
{
if(selections(i))
{
picked.push_back(original(i));
}
}
//put the vector values back into an eigen array
ArrayXi theGoodStuff = Map<ArrayXi, Unaligned>
(picked.data(), picked.size());
cout<<"Just the good stuff:"<<endl
<<theGoodStuff.format(OctaveFmt)<<endl;
</code></pre>
<p>Here's the output I get:</p>
<pre><code>Original with bad values:
[1 2 3 4]
One if it's a good value:
[1 1 0 0]
Just the good stuff:
[1 2]
</code></pre>
<p>Does anyone know how to do this in a more 'eigen' way, or just a faster way than looping through the arrays?</p>
More answer... (https://stackoverflow.com/questions/59060596/is-there-a-better-way-to-implement-matlabs-logical-indexing-using-eigen-c)
<pre><code>//an attempt at matlab-style Logical Indexing
//equivalent to the matlab:
// original = [1,2,3,4]
// subset = original(original < 3)
using namespace Eigen;
using std::cout;
using std::endl;
IOFormat OctaveFmt(StreamPrecision, 0, ", ", " ", "", "", "[", "]");
ArrayXi original(4);
original << 1,2,3,4;
cout<<"Original with bad values:"<<endl
<<original.format(OctaveFmt)<<endl;
Array<bool, Dynamic,1> selections = original < 3;
cout<<"One if it's a good value:"<<endl
<<selections.format(OctaveFmt)<<endl;
std::vector<int> picked;
for(int i = 0; i < selections.size(); i++ )
{
if(selections(i))
{
picked.push_back(original(i));
}
}
//put the vector values back into an eigen array
ArrayXi theGoodStuff = Map<ArrayXi, Unaligned>
(picked.data(), picked.size());
cout<<"Just the good stuff:"<<endl
<<theGoodStuff.format(OctaveFmt)<<endl;
</code></pre>
<p>Here's the output I get:</p>
<pre><code>Original with bad values:
[1 2 3 4]
One if it's a good value:
[1 1 0 0]
Just the good stuff:
[1 2]
</code></pre>
<p>Does anyone know how to do this in a more 'eigen' way, or just a faster way than looping through the arrays?</p>
More answer... (https://stackoverflow.com/questions/59060596/is-there-a-better-way-to-implement-matlabs-logical-indexing-using-eigen-c)