Posted By

ManishChandraReddy on 03/29/16


Versions (?)

Array total comparision

 / Published in: Python


Compare two arrays wholly. Useful for setting them in regions.

  1. (A==B).all()
  2. test if all values of array (A==B) are True.
  4. Edit (from dbaupp's answer and yoavram's comment)
  6. It should be noted that:
  8. 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.
  9. Another risk is if A and B don't have the same shape and aren't broadcastable, then this approach will raise an error.
  10. 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:
  12. np.array_equal(A,B) # test if same shape, same elements values
  13. np.array_equiv(A,B) # test if broadcastable shape, same elements values
  14. np.allclose(A,B,...) # test if same shape, elements have close enough values

Report this snippet  

You need to login to post a comment.