I’m using C++ and want to combine the masking of a vector and the search for the indexes where a condition is verified, similarly to the numpy where function.
Here’s an example:
JavaScript
x
5
1
std::vector<int> id = {61, 66, 68, 80}
2
std::vector<int> val = {0, 0, 2, 1};
3
std::vector<int> offset = {62, 65, 70};
4
std::vector<int> masked;
5
After using
JavaScript
1
2
1
masked = offset[val];
2
masked
should look like this: masked = {62, 62, 70, 65}
Afterwards, I want to find the indexes where id
vector elements are greater than masked
ones. Both vectors have the same length. This is equivalent to Numpy’s where function in Python.
JavaScript
1
2
1
c = np.where(id > masked)[0]
2
c
is an equivalent of vector<int>
and should look like this: c = {1,3}
Any ideas out there? Thank you!
Advertisement
Answer
You can just use a simple loop. Note that you do not need the intermediate array masked
. Here is an (untested) example:
JavaScript
1
7
1
std::vector<int> c;
2
c.reserve(val.size());
3
4
for(size_t i=0 ; i<val.size() ; ++i)
5
if(id[i] > Offset[val[i]])
6
c.push_back(i);
7