This is probably the fastest solution, but the where function throws an error because it pre-computes the solutions:
import numpy as np n = 4 xy = np.random.randint(4, size=(n,n)).astype(float) x_norm = np.random.randint(4, size=(n,n)).astype(float) y_norm = np.random.randint(4, size=(n,n)).astype(float) xy_norm = x_norm*y_norm edge_map = np.where(xy_norm == 0, xy_norm, xy/xy_norm) print(xy) print(xy_norm) print(edge_map)
Chiel source share