Andy
Andy

Reputation: 19261

Numpy compare values inside to return greater index

I have a numpy array and another array:

[array([-1.67397643, -2.77258872]), array([-1.67397643, -2.77258872]), array([-2.77258872, -1.67397643]), array([-2.77258872, -1.67397643])]

  1. Which index position inside the numpy arrays wins - i.e. -1.67397643 > -2.77258872 - so the first value would be 0.
  2. Final output of the numpy array would be [0, 0, 1, 1] (a list is fine too)

How can I do that ?

Upvotes: 0

Views: 35

Answers (1)

VBB
VBB

Reputation: 1325

It seems you have a list of arrays, so I would start by making them a proper numpy array:

a = [array([-1.67397643, -2.77258872]), array([-1.67397643, -2.77258872]), array([-2.77258872, -1.67397643]), array([-2.77258872, -1.67397643])]
b = np.array(a).T # .T transposes it.
c = b[0] < b[1]

c is now an array([False, False, True, True], dtype=bool), and probably serves your purpose. If you must have [0,0,1,1] instead, then:

d = np.zeros(len(c))
d[c] = 1

d is now an array([ 0., 0., 1., 1.])

Upvotes: 1

Related Questions