0
votes

I have a collection of data on which i would like to apply while loop but i got an error

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

import numpy as np


data = np.loadtxt('data4.txt')
i=np.array(data[:,1])    #column having thousand values 

j=7/2

f = i-j

while f <= i+j:
    print(f)
    f = f+1
2
Have you tried searching for that error on Google? It's not clear what you're trying to do but this is equivalent to saying if 10 <= [2, 4, 6, 8, 10, 12]. How is that to be interpreted? - roganjosh
Can you clarify at which line the error is thrown? - kosnik
at this line "while f <= i+j:" - Hamza Hanif
@roganjosh: I am trying to range of "i-j, (i-j)+1,(i-j)+2 .... ,i+j" where j is constant while 'i' have different values - Hamza Hanif
How do you end with i+j in that? Maybe (i-j)+2j? But anyway, you're still applying that to an array, so your intent is not clear to me. Should it stop when one value in that array crosses the threshold, or all of them? We can't see your input data. - roganjosh

2 Answers

1
votes

Have you tried to use this? The solution might be in the output actually. Hope this works for you

import numpy as np  
data = np.loadtxt('data4.txt')
i=np.array(data[:,1])    #column having thousand values 

j=7/2   
f = i-j
while np.all(f <= i+j):
    print(f)
    f += 1
0
votes

You may have some data in the f array evaluate to True for being less than or equal to the Cartesian Sum of i and j, while some data in the f array evaluate to False.

So which one do you pick for truthiness? That's where any() and all() come into play:


Any

condition = i + j

while not (f - condition).any():  # If any elements of f are not greater than i + j
    ...
    condition = i + j

All

condition = i + j

while not (f - condition).all():  # If not all of the elements of f are greater than i + j
    ...
    condition = i + j