Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

So I’m trying to create an array of the same size as [A]. What I want is a forloop that checks if the 1st value of the ith element in the Array == 0, However it keeps telling me that there is more than one element in the truth value of an array when there shouldnt be as I indexed the 1st value of the ith element in my array. Here is my code:

n = 4
N = [i for i in range(1,n+1)]
V = [0] + N
K = N + [5]
M = [0,1]
A = np.array([(i,j,k) for i in V for j in K for k in M if i!=j])

C=[]

for i in A:
    if A[i][0]==0:
        C.append([0.7])
    elif abs(A[i][0]-A[i][1])<=1:
        C.append([1])
    else:
        C.append([0])

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When you go through your for loop, i is already each list in A, you can check this with the below:

for i in A:
    print(i)

Which returns:

[0 1 0]
[0 1 1]
[0 2 0]
[0 2 1]...

So then calling A[i][0] gives an array each time rather than an integer, so the comparison A[i][0] == 0 is not possible. To fix your problem, either do the below, which will change your i to get an index for every element in A:

for i in range(len(A)):
    if A[i][0]==0:
        C.append([0.7])
    elif abs(A[i][0]-A[i][1])<=1:
        C.append([1])
    else:
        C.append([0])

Or change all instances of A[i][x] to i[x], and use the each list element of A that way, as follows:

for i in A:
    if i[0]==0:
        C.append([0.7])
    elif abs(i[0]-i[1])<=1:
        C.append([1])
    else:
        C.append([0])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading