Q:

Python/NumPy: Return a List of Smaller (LHS) + Larger Elements (RHS) out of the Two Lists

Hie all, I would like to do something simple, which is comparing A and B then return the smaller element on the left hand column (column 0) and the larger element on the right hand column (column 1).
Say,
import numpy as np
A = np.array([[  311.,360.],
             [-4022.,-3973.],
             [   96.,145.],
             [ 3989.,4038.]])

and,
B = np.array([[310.,460.],
             [ -4018.,4013.],
             [ -297.,-256.],
             [ 4005.,4039.]])

I would like to get:-
C = [[  310.,460.],
     [-4022.,4013.],
     [  -297.,145.],
     [ 3989.,4039.]]

I tried A[A>B] but the positions of the elements are not in order. Is there a sleek way to solve this? Thank you for your attention and kind help!

A:

With A[A>B] you get a list of all elements of A which are larger than the corresponding elements of B.
Use instead
>>> idx1 = A[:,0]<B[:,0]
>>> idx2 = A[:,1]>B[:,1]
>>> idx = np.column_stack((idx1,idx2))
>>> np.where(idx,A,B)
array([[  310.,   460.],
       [-4022.,  4013.],
       [ -297.,   145.],
       [ 3989.,  4039.]])

