I need to obtain the covariance between an array a of shape (M1, M2, N) and another array b of shape (N,).
What I currently do is use a for block:
import numpy as np
M1, M2, N = 23, 50, 117
a = np.random.uniform(-2., 2., (M1, M2, N))
b = np.random.uniform(-1., 1., N)
c = []
for i in range(M1):
for j in range(M2):
c.append(np.cov(a[i][j], b)[0, 1])
but it gets a bit slow for large (M1, M2). Is there a way to speed this up?