Ceil division of a numpy array.

huangapple go评论99阅读模式
英文:

Ceil division of a numpy array

问题

Ceil division of a numpy array

我想要将一个numpy数组除以2并返回向上取整的值。
换句话说,将一个值除以2并四舍五入。

>>> a = np.array([1, 2, 3, 4])
>>> np.ceil_divide(a, 2)
array([1, 1, 2, 2])
英文:

Ceil division of a numpy array

I want to divide a numpy array by 2 and return the ceilings values.
In other words, divide a value by two and round up.

>>> a = np.array([1, 2, 3, 4])
>>> np.ceil_divide(a, 2)
array([1, 1, 2, 2])

答案1

得分: 1

只使用 numpy.ceil

out = np.ceil(a/2)

输出:array([1., 1., 2., 2.])

或者最终:

out = np.ceil(a/2).astype(a.dtype)

输出:array([1, 1, 2, 2])

英文:

Just use numpy.ceil:

out = np.ceil(a/2)

Output: array([1., 1., 2., 2.])

Or eventually:

out = np.ceil(a/2).astype(a.dtype)

Output: array([1, 1, 2, 2])

huangapple
  • 本文由 发表于 2023年5月23日 01:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定