/ Published in: Python
Compare two arrays wholly. Useful for setting them in regions.
- test if all values of array (A==B) are True.
- Edit (from dbaupp's answer and yoavram's comment)
- It should be noted that:
- this solution can have a strange behavior in a particular case: if either A or B is empty and the other one contains a single element, then it return True. For some reason, the comparison A==B returns an empty array, for which the all operator returns True.
- Another risk is if A and B don't have the same shape and aren't broadcastable, then this approach will raise an error.
- In conclusion, the solution I proposed is the standard one, I think, but if you have a doubt about A and B shape or simply want to be safe: use one of the specialized functions:
- np.array_equal(A,B) # test if same shape, same elements values
- np.array_equiv(A,B) # test if broadcastable shape, same elements values
- np.allclose(A,B,...) # test if same shape, elements have close enough values
You need to login to post a comment.