简体   繁体   English

针对以下问题的 numpy arrays 的迭代

[英]Iterations for the numpy arrays for following problem

i have 3 numpy arrays a,c and m, i want a loop which takes every element from a,c,m in each loop. i have 3 numpy arrays a,c and m, i want a loop which takes every element from a,c,m in each loop. For example in first iteration it takes 4,3,6 and executes z=(a*2+c)%m five times then in second it takes 5,5,5 and execute same expression 5 times.例如,在第一次迭代中需要4,3,6并执行z=(a*2+c)%m五次,然后在第二次中需要5,5,5并执行相同的表达式 5 次。 i want random number generation.我想要随机数生成。 see code below见下面的代码

a = [[4, 5, 1], [4, 3, 1], [6, 7, 2]]

c = [[3, 5, 1], [4, 3, 1], [6, 7, 2]]

m = [[6, 5, 1], [4, 3, 1], [6, 7, 2]]

for i in range(0, 5):
    z = (a * 2 + c) % m
for i, j, k in zip(a, c, m):
    for l, n, o in zip(i, j, k):
        for i in range(0,5)
            z=(a*2+c)%m
it = np.nditer(a, flags=['multi_index'])
for x in it:
  print("%d%d%d "%(a[it.multi_index],c[it.multi_index],m[it.multi_index]))

I don't know the reason why you want to extract the elements from those matrix, this is a simple solution:我不知道为什么要从这些矩阵中提取元素,这是一个简单的解决方案:

for i in range(a.shape[1]):
  for j in range(b.shape[0]):
    print([a[i,j], b[i,j], c[i,j]])

If you are interested in doing advanced operations with them like matrix multiplication,..., I recommend you to use the slice techniques offerd by Numpy.如果您有兴趣使用它们进行矩阵乘法等高级操作,...,我建议您使用 Numpy 提供的切片技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM