Suppose an array a.shape == (N, M)
and a vector v.shape == (N,)
. The goal is to compute argmin
of abs
of v
subtracted from every element of a
- that is,
out = np.zeros(N, M)
for i in range(N):
for j in range(M):
out[i, j] = np.argmin(np.abs(a[i, j] - v))
I have a vectorized implementation via np.matlib.repmat
, and it's much faster, but takes O(M*N^2)
memory, unacceptable in practice. Computation's still done on CPU so best bet seems to be implementing the for-loop in C as an extension, but maybe Numpy already has this logic implemented.
Does it? Any use-ready Numpy functions implementing above efficiently?