0

I have two python ndarrays arr1 and arr2 as follows:

    import numpy as np
    arr1 = np.array([1.        , 1.        , 0.1862802 , 0.19957115, 0.18623812,
           0.1802321 , 0.17464815, 0.16460853, 0.1487719 , 0.12968006,
           0.10464501, 0.07183418, 0.00124706, 0.27353592, 0.81713212,
           0.23720725, 0.21802175, 0.21959138, 0.22401754, 0.22662527,
           0.22777369, 0.23269387, 0.23293132, 0.23374038, 0.24089565,
           0.19958937, 0.23910928, 0.24252447])

arr2 = np.array([[ 1.        ,  1.        ],
       [-0.357316  ,  0.1862802 ],
       [-0.34402505,  0.19957115],
       [-0.35735808,  0.18623812],
       [-0.36336411,  0.1802321 ],
       [-0.36894805,  0.17464815],
       [-0.37898767,  0.16460853],
       [-0.3948243 ,  0.1487719 ],
       [-0.41391615,  0.12968006],
       [-0.4389512 ,  0.10464501],
       [-0.47176202,  0.07183418],
       [-0.54234915,  0.00124706],
       [ 0.27353592,  0.81713212],
       [-0.30638895,  0.23720725],
       [-0.32557445,  0.21802175],
       [-0.32400482,  0.21959138],
       [-0.31957866,  0.22401754],
       [-0.31697093,  0.22662527],
       [-0.31582252,  0.22777369],
       [-0.31090234,  0.23269387],
       [-0.31066488,  0.23293132],
       [-0.30985582,  0.23374038],
       [-0.30270055,  0.24089565],
       [-0.34400684,  0.19958937],
       [-0.30448692,  0.23910928],
       [-0.30107173,  0.24252447]])

I want to get (a) all the values in arr1 where first value of corresponding tuple in arr2 is >0 and (b) the index of all those values in arr1

1
  • 1
    The first array has length 28 and the second 26. Should't they have the same length? Commented May 28, 2021 at 8:30

2 Answers 2

1

A simple solution by just storing the arr1 indices that matches the a) conditions.



#Define the  output list
arr1_index = []

#Loop on arr1,arr2
for i,(a1,a2) in enumerate(zip(arr1,arr2)):
    #(a) all the values in arr1 where first value of corresponding tuple in arr2 is >0 
    #(b) the index of all those values in arr1
    # Just need to store indices from arr1 that match the a) conditions
    if a2[0]>0:
        arr1_index += [i]
   
print(arr1_index)
print(arr1[arr1_index])

Sign up to request clarification or add additional context in comments.

1 Comment

it should work on arrays of different size, just keep in mind that the longest array will be truncated to the smallest one size
1

To get the values you can just use indexing with boolean arrays:

a = arr1[arr2[:,0] > 0]

To get the indices, use the nonzero function:

b = np.nonzero(arr2[:,0] > 0)[0]

However, your this won't work in your example arrays, as they don't have the same length. arr1 has length 28 and arr2 length 26.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.