i used if else to check if the element at index i,i of a numpy matrix is 0
if a[i, i]==0:
got the error
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Is there any workaround for this?
The problem with your code is that a[i, i] is not a single value rather a list or numpy array. And you can't check with just one if condition all values in a list/numpy array.
What you could do is:
for item in a.flatten():
if item == =.
# do sth
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more
if any(a[i, i]==0)
youra[i,i]
Has apparently more than 1 dimension – Grzegorz Skibinski