How to handle vector like in numpy?

Hello everyone, in python, I often use this construction from numpy to select the necessary elements of an array:

counter = (array >= threshold).sum()

Is there something similar for vectors in julia or is it just to use PyCall?

There are a number of ways to do this. sum(a for a in array if a>=threshold) is probably the best, but if you want a more numpy-like syntax, you could use sum(array.>=threshold) (although this will probably be slower than the comprehension)

3 Likes

the first option is not exactly what I need, the values are added there, and I do not need the values themselves, but their number
The second option works just fine, thanks!

You can simply do:

counter = sum(>=(threshold), array)
4 Likes

Or if you like typing more and being explicit:

count(>=(threshold), array)
3 Likes

oh, then replacing sum with count would fix mine.

1 Like