How do I apply some function to a python meshgrid?

I’d use numpy.vectorize to “vectorize” your function. Note that despite the name, vectorize is not intended to make your code run faster — Just simplify it a bit.

Here’s some examples:

>>> import numpy as np
>>> @np.vectorize
... def foo(a, b):
...    return a + b
... 
>>> foo([1,3,5], [2,4,6])
array([ 3,  7, 11])
>>> foo(np.arange(9).reshape(3,3), np.arange(9).reshape(3,3))
array([[ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16]])

With your code, it should be enough to decorate func with np.vectorize and then you can probably just call it as func(X, Y) — No raveling or reshapeing necessary:

import numpy as np
import collections as c

# some arbitrary lookup table
a = c.defaultdict(int)
a[1] = 2
a[2] = 3
a[3] = 2
a[4] = 3

@np.vectorize
def func(x,y):
    # some arbitrary function
    return a[x] + a[y]

X,Y = np.mgrid[1:3, 1:4]
X = X.T
Y = Y.T

Z = func(X, Y)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)