mommomonthewind
mommomonthewind

Reputation: 4640

Python: count how many times an element of a vector is in between of elements of two other vectors?

Say I have 3 vectors:

v1 = 1,6,7

v2 = 2,5,6

v3 = 3,4,2

I want to count how many times that v1[i] <= v2[i] <= v3[i] (in a Pythonic way of course). For the above example, the answer should be 1.

Upvotes: 0

Views: 68

Answers (2)

T. Kau
T. Kau

Reputation: 643

If the v1, v2, and so on arenumpy.arrays you can do

np.sum(np.logical_and(v1<v2, v2<v3))

Upvotes: 0

Carles Mitjans
Carles Mitjans

Reputation: 4866

Try this:

sum(v1[x] <= v2[x] <= v3[x] for x in range(3))

Upvotes: 1

Related Questions