-2

I was trying to write the following code:

def ordering(n):
    eqvec=np.zeros(n)
    bvec=[]
    for i in range(n):
        eqvec[i]=orderingeq(n,i+1)
    while eqvec:
        index_max=np.argmax(eqvec)
        bvec.append(index_max)
        eqvec.remove(index_max)
    return bvec

In this code I have two vectors. The first vector 'eqvec' contains some numbers which are determined with the equation 'orderingeq'. From this point I need to fill the vector 'bvec' with the index values of maximum of the vector 'eqvec'. From what I have found online is that this should work, or at least I have not found an alternate way to do this. But I get the error message corresponding to the line 'while eqvec:'.

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

Could someone help me by telling me what is wrong with my code and maybe tell me how it should be done?

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
TK99
  • 123
  • 4

2 Answers2

0

This is the correct way to check whether a numpy array is empty or not. Try:

while eqvec.size != 0:

Be aware that if eqvec is a list instead of a numpy array this doesn't work!

frblazquez
  • 115
  • 1
  • 10
0

I think bvec = np.flip(np.argsort(eqvec)) would give you what you need. np.argsort(eqvec) would give you an array of the index locations, starting from smallest to largest, and then np.flip would reverse the index locations so instead your position of the largest element is first, second largest is second etc.

If you'd rather the result be a in a list then you can just pass that into list.

ap1997
  • 183
  • 6