使用函数的多维数组参数的索引

时间:2016-04-18 21:07:27

标签: python arrays numpy multidimensional-array

我遇到以下问题而且找不到任何解决方案。

    for index, value in numpy.ndenumerate(re):
        re[index] += rec[(index)]

不幸的是,这并没有奏效。该函数如下所示:

def rec(x1, x2, x3, y1, y2, y3, z1, z2, z3):

索引的大小确实适合函数的参数个数。我希望尽可能保持最佳状态,因此

for [x1][x2][x3][y1][y2][y3][z1][z2][z3], value in numpy.ndenumerate(re):
        re[index] += rec[(index)]

对我没有帮助。你知道吗?

2 个答案:

答案 0 :(得分:0)

2D示例(Ipython notebook)

import numpy as np
import matplotlib.pyplot as p # just for visualization
%matplotlib inline

x=np.arange(0,1,0.01)  # <-- give ranges to your parameters
y=np.arange(0,1,0.01)
xv, yv = np.meshgrid(x,y)   # <--- generate the n-dimensional indeces

zv= np.sin(xv + 3* yv)  # <-- this is the ufunc, look ma: no for loops

p.imshow(zv,aspect ='1') 

enter image description here

答案 1 :(得分:0)

rec不是描述问题的正确方法。你需要告诉我们你得到的错误。

但我猜这与__getitem__不可索引或拥有def rec(x1, x2, x3, y1, y2, y3, z1, z2, z3): .... 方法有关。

您定义了一个包含大量参数的函数:

rec(1,2,3,....) # should work

然后你必须把它称为一个函数。它不是列表或数组。

index = (1,2,3,...)
rec(*index)

for index, value in numpy.ndenumerate(re):
        re[index] += rec(*index)

如果是这样,那么以下内容可能有效:

table