1

I have a ndarray of x and y component of 2D speed values of an object, such as:

(Pdb) p highspeedentries
array([[  0.  ,  52.57],
   [-40.58,  70.89],
   [-57.32,  76.47],
   [-57.92,  65.1 ],
   [-52.47,  96.68],
   [-45.58,  77.12],
   [-37.83,  69.52]])

(Pdb) p type(highspeedentries)
<class 'numpy.ndarray'>

I have to check if there is any row that contains a value higher than 55 for the first and second components in the row. I also need to take the absolute value for any negative speed component, but first I tried the following and all of them give errors.

(Pdb) p highspeedentries[highspeedentries[:,1] > 55 and highspeedentries[0,:] > 55]
*** ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

(Pdb) p highspeedentries[highspeedentries[:,1] > 55 & highspeedentries[0,:] > 55]
*** TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be 
safely coerced to any supported types according to the casting rule ''safe''

(Pdb) p highspeedentries[np.logical_and(highspeedentries[:,1] > 55, highspeedentries[0,:] > 55)]
*** ValueError: operands could not be broadcast together with shapes (7,) (2,)
1
  • highspeedentries[0,:] > 55 replace with highspeedentries[:,0] > 55 Commented Nov 14, 2019 at 0:04

1 Answer 1

1

One dumb way of approaching it is to just loop over the entire array looking for your state:

for i in highspeedentries:
    if (abs(i[0]) > 55 and abs(i[1]) > 55):
        print('True')
        break
else:
    print('False')

Alternatively, you were on the right track for your third attempt:

logical_and(abs(highspeedentries[:, 0]) > 55 , abs(highspeedentries[:, 1]) > 55).any()

The order of indices was incorrect, and if you add .any() to the end you get a single value (True if at least one element is True, False otherwise) instead of a boolean array. To apply the absolute value, you can just apply abs() to the arrays before comparing with 55.

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

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.