簡體   English   中英

具有多個元素的數組的真值是不明確的(數字的)

[英]The truth value of an array with more than one element is ambiguous (numerical)

我陷入了錯誤,無法設法找到解決方案。 這是我的功能:

def rep(N,list,t):
     result=0
     for i in range(N):
         if((list[i]-t)<=t):
             result = result + 1
         else:
             result = result + 0
     return(result/N) 

然后我嘗試繪制函數:

 plt.plot(x,rep(1000,nEchant,x))

這就是我得到的:

 Traceback (most recent call last):
   File "<pyshell#114>", line 1, in <module>
     plt.plot(x,rep(1000,nEchant,x))
   File "<pyshell#113>", line 4, in rep
     if((list[i]-1)<=t):
 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

謝謝!

看起來表達式(list[i]-t)<=t返回一個numpy數組。 為確保list[i]-t 所有值均小於t ,請使用:

numpy.all((list[i]-t)<=t)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM