[Numpy-discussion] all elements equal

Zachary Pincus zachary.pincus@yale....
Mon Mar 5 13:28:41 CST 2012


How about the following?
exact: numpy.all(a == a[0])
inexact: numpy.allclose(a, a[0])

On Mar 5, 2012, at 2:19 PM, Keith Goodman wrote:

> On Mon, Mar 5, 2012 at 11:14 AM, Neal Becker <ndbecker2@gmail.com> wrote:
>> What is a simple, efficient way to determine if all elements in an array (in my
>> case, 1D) are equal?  How about close?
> 
> For the exactly equal case, how about:
> 
> I[1] a = np.array([1,1,1,1])
> I[2] np.unique(a).size
> O[2] 1    # All equal
> 
> I[3] a = np.array([1,1,1,2])
> I[4] np.unique(a).size
> O[4] 2   # All not equal
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list