get all the indexes by a condition
Issue #362
resolved
Hi
i am new to blaze and i am trying to get all the indexes from a vector with a condition
for example in python with numpy :
a = np.array([4, 5, 6, 2, 3, 4])
b = np.where(a==4)
#b will be [0,5]
print(b)
is there a similar implementation in blaze
i also know about std::find_if which can be used to find an element by a condition in a vector and can be used to find all the elements
for example :
std::vector<int> a = {4,5,6,2,3,4};
std::vector<int> b;
auto it = std::find_if(std::begin(a), std::end(a), [](int i){return i == 4;});
while (it != std::end(a)) {
b.emplace_back(std::distance(std::begin(a), it));
it = std::find_if(std::next(it), std::end(a), [](int i){return i == 4;});
}
//b will be {0,5}
for (std::vector<int>::const_iterator i = b.begin(); i != b.end(); ++i)
std::cout << *i << ' ';
my thanks
edward
Comments (3)
-
-
reporter hi Klaus
your answer solved my issue my thanks
edward -
reporter - changed status to resolved
- Log in to comment
Hi Edward!
Thanks for creating this issue. Unfortunately Blaze doesn’t have any operation that could be used for the purpose of selecting indices. The best option at this point is to implement this operation manually:
Note that this operation always returns a
DynamicVector
, because the number of resulting indices is unknown at compile time. As a consequence, this function will always allocate.We’ll see how we can provide a generic operation that can be used for this purpose. Thanks again for creating this issue,
Best regards,
Klaus!