
怎么用numpy指令来写图中的def啊,如何用numpy的形式,不用for和while循环等操作来达到图中def一样的功能?
import numpy as np
def c(x, y, z):
x1 = x.reshape((len(x), 1))
y1 = y.reshape((1, len(y)))
a = x1 - y1
q = np.where(a > 0, z, 0)
q += np.where(a <= 0, z, 0)**2
return q
x = np.arange(3)
print("x=:\n",x)
y = np.arange(4)
print("y=:\n", y)
z = np.arange(12).reshape(3, 4)
print("z=:\n", z)
q = c(x, y, z)
print("q=:\n", q)
x=:
[0 1 2]
y=:
[0 1 2 3]
z=:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
q=:
[[ 0 1 4 9]
[ 4 25 36 49]
[ 8 9 100 121]]
不知道你这个问题是否已经解决, 如果还没有解决的话:
如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^