I have two vectors and I would like to construct a matrix of their pairwise differences. Currently I do this:
import numpy as np
a = np.array([1,2,3,4])
b = np.array([3,2,1])
M = a.reshape((-1,1)) - b.reshape((1,-1))
This certainly works, but I wonder if it's really the intended way of doing things. The readability of the line is suboptimal; one has to think a while what the reshapes are doing. Can this be improved? Is there another "cleaner" way of achieving the same?