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)


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)

Or if you like typing more and being explicit:

count(>=(threshold), array)

oh, then replacing sum with count would fix mine.

1 Like