Problem: Given a vector $X \in \mathbb{R}^n$ compute the pairwise distance matrix: $M_{ij} = X_i - X_j$ # For Loop solution (slow) ```python M = np.zeros((n,n)) for i in range(n): for j in range(n): M[i,j] = X[i] - X[j] ``` When $n=10,000$, this takes 50 seconds on Google colab https://colab.research.google.com/drive/1SM6yu7O6W6HA6AJcVmcCbclNtM2iBym0?usp=sharing # Array Broadcasting Solution ```python M = X[:,np.newaxis] - X[np.newaxis,:] ``` When $n=10,000$, this takes 0.2 seconds (a 250x speedup compared to the for loop solution)