簡體   English   中英

numpy數組而不是float

[英]Numpy array instead of float

我寫了一個簡單的函數

def rect(t):
    field = np.zeros((len(t),3))
    for i in range(len(t)):
        if t[i] >=-10 and t[i] <=10:
            field[i,1] = 1
        else:
            field[i,1] = 0
    return field

並想要整合它

from scipy import integrate

def S_elem(t):
    return rect(t)[:,1]*integr

integrate.quad(S_elem, -10, 10)

我得到這個錯誤

TypeError: object of type 'float' has no len()

我知道我可能不使用numpy數組,但出於其他目的,我需要它。 如何在不刪除numpy數組類型的情況下執行集成?

您必須將float轉換為numpy數組:

def rect(t):
    t = np.atleast_1d(t)
    field = np.zeros((len(t), 3))
    field[:, 1] = abs(t) <= 10
    return field

暫無
暫無

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

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