Search code examples
pythonalgorithmscipydistancearray-broadcasting

calculate difference between all combinations of entries in a vector


I have a numpy 1D array of z values, and I want to calculate the difference between all combinations of the entries, with the output as a square matrix.

I know how to calculate this as a distance between all combinations of the points using cdist, but that does not give me the sign:

So for example if my z vector is [1,5,8]

import numpy as np
from scipy.spatial.distance import cdist

z=np.array([1, 5, 8])
z2=np.column_stack((z,np.zeros(3)))
cdist(z2,z2)

gives me:

array([[0., 4., 7.],
       [4., 0., 3.],
       [7., 3., 0.]])

but I want to have signs to give me:

array([[0., 4., 7.],
       [-4., 0., 3.],
       [-7., -3., 0.]])

I thought about fudging things by using np.tril_indices to flip the sign of the lower triangle, but this won't work, as I need the pairs to be differenced in a consistent way for my operation (i.e. if I perform this on two or more vectors, the pairs are always compared in the same order), whereas by flipping the sign I will always have positive differences in the upper right and negative in the lower left.


Solution

  • Simple one line solution using numpy array broadcasting.

    import numpy as np
    
    z = np.array([1, 5, 8])
    # Simple one line solution
    z - z.reshape(-1,1)
    

    Output:

    array([[ 0,  4,  7],
           [-4,  0,  3],
           [-7, -3,  0]])