![]() |
Is there a better way to implement matlab's Logical Indexing using Eigen/C++?
<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> [url=https://stackoverflow.com/questions/59060596/is-there-a-better-way-to-implement-matlabs-logical-indexing-using-eigen-c]More answer...[/url] |
所有时间均为北京时间。现在的时间是 21:22。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.