python - Vectorizing three nested loops - NumPy -


i have 2 arrays x.dim = (n,4) , y.dim = (m, m, 2) , function f(a, b), takes k , ldimensional vectors respectively arguments. want obtain array res.dim = (n, m, m) such that

for n in range(n):   in range(m):     j in range(m):       res[n, i, j] = f(x[n], y[i, j]) 

can't how use apply in case. in advance help!

def f(a, b):     return max(0, 1 - np.sum(np.square(np.divide(np.subtract(b, a[0:2]), a[2:4])))) 

here's vectorized approach work listed function using numpy broadcasting , slicing -

# slice out relevant cols x x_slice1 = x[:,none,none,:2] x_slice2 = x[:,none,none,2:4]  # perform operations using slices correspond iterative operations out = np.maximum(0,1-(np.divide(y-x_slice1,x_slice2)**2).sum(3)) 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -