Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lexicographic comparison of two numpy ndarrays

I couldn't find a straightforward way to compare two (multidimensional in my case) arrays the in a lexicographic way.

Ie.

a = [1,2,3,4]
b = [4,0,1,6]

For a < b I want to get true where I get [true, false, false, true]
For a > b I want to get false where I get [false, true, true, false]

like image 568
Adam Toth Avatar asked Oct 18 '22 06:10

Adam Toth


1 Answers

If the question is just about finding whether a is < or > than b, then the following should work.

def fn(a, b):
    # finds index of the first non matching element
    idx = np.where( (a>b) != (a<b) )[0][0]

    if a[idx] < b[idx]: print "a < b" 
    if a[idx] > b[idx]: print "a > b" 
like image 110
Rahul Avatar answered Oct 21 '22 05:10

Rahul