How to find the pairwise differences between rows of two very large matrices using numpy?
Given two matrices, I want to compute the pairwise differences between all rows. Each matrix has 1000 rows and 100 columns so they are fairly large. I tried using a for loop and pure broadcasting but the for loop seem to be working faster. Am I doing something wrong? Here is the code: from numpy import * A = random.randn(1000,100) B = random.randn(1000,100) start = time.time() for a in A: sum((a - B)**2,1) print time.time() - start # pure broadcasting start = time.time() ((A[:,newaxis,:] - B)**2).sum(-1) print time.time() - start The broadcasting method takes about 1 second longer and it's