sten
sten

Reputation: 7476

Compare two arrays with different size, elementwise?

Let say I have two array of different sizes :

In [79]: tr
Out[79]: array([1, 1, 0, 6, 0, 3])

In [80]: br
Out[80]: array([ 9, 26, 24, 18, 14, 12,  8])

I want to make sure all elements of br are bigger than all elements of tr i.e. br > tr

 ValueError: operands could not be broadcast together with shapes (6,) (7,) 

Upvotes: 0

Views: 417

Answers (3)

Nicolas Gervais
Nicolas Gervais

Reputation: 36624

If it makes logical sense for your task, you could pad your shorter array with zeros:

import numpy as np

a = np.array([1, 1, 0, 6, 0, 3])
b = np.array([ 9, 26, 24, 18, 14, 12,  8])

all(np.less_equal(np.pad(a,  (0, len(b) - len(a))), b))
True

What the smaller array looks like when padded:

array([1, 1, 0, 6, 0, 3, 0])

Upvotes: 1

Zesty Dragon
Zesty Dragon

Reputation: 561

As you can see i have created 2 loops first for bigger size array and second for small size array

   for i in range(0,len(br)):
        for j in range(0,len(tr)):
            if(br[i]<tr[j]):
               #tr[j] is bigger than value in br[i]

Upvotes: 0

Giuseppe
Giuseppe

Reputation: 678

You can simply check if the minimum number in br is bigger than the maximum number in tr:

if min(br) > max(tr):
    # all the element in br are bigger 
else:
    # there is at least one value in tr bigger or equal than one value in br

Upvotes: 1

Related Questions