p4 = N * (A ** 2).sum(0) - sA ** 2
pcorr = (p1 - p2) / np.sqrt(p4 * p3[:, (None)])
out = pcorr[np.nanargmax(np.abs(pcorr), axis=0), np.arange(pcorr.shape[1])]
Efficient pairwise correlation for two matrices of features