遇到一个对应计算的问题

2019-08-13 16:19:12 +08:00
 love642285
我想生成一串随机矩阵,用了 for 循环生成了 s0,s1,...sN 和 e0,e1,...,eN
然后想对其进行计算,使得 bi=si*ei,但是用 for 循环写不出来
B = np.random.randint(0,q,(m,n))
for i in range(0,N):
exec("s%s=np.random.randint(0,q,(n,1))"%i)
for i in range(0,N):
exec("e%s=np.random.randint(0,q,(m,1))"%i)
for i in range(0,N):
exec("b%s=np.dot(B,s%s)+e%s"%i)

我已经生成了 s0 到 sN 和 e0 到 eN 了,也可以 print,但是调用他们计算 bi 的时候会报错
exec("b%s=np.dot(B,s%s)+e%s"%i)
TypeError: not enough arguments for format string
1396 次点击
所在节点    Python
3 条回复
cherbim
2019-08-13 20:56:52 +08:00
not enough arguments for format string
这个圈起来,期末考试要考,话说你知道这个报错啥意思么
cherbim
2019-08-13 21:02:45 +08:00
最后一行你要传递三个参数,虽然三个参数相同,那也不能省略
necomancer
2019-08-13 21:16:54 +08:00
B = np.random.randint(0,q,(m, n))
s = np.random.randint(0,q,(N, n))
e = np.random.randint(0,q,(N, m))
b = np.einsum('ij,...j->...i', B, s) + e

b -> (N, m)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/591550

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX