开发者

mean of elements i and i+1 in a numpy array

开发者 https://www.devze.com 2023-03-03 18:09 出处:网络
Out of curiosity, is there a specific numpy function to do the following (which would supposedly be faster):

Out of curiosity, is there a specific numpy function to do the following (which would supposedly be faster):

a = np.array((开发者_Go百科0,2,4))
b = np.zeros(len(a) - 1)
for i in range(len(b)):
    b[i] = a[i:i+2].mean()

print(b)
#prints [1,3]

Cheers


You could use

b = (a[1:] + a[:-1]) / 2.

to avoid the Python loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号