So you'd be making about twice the number of operations you'd do in a loop, and you'd occupy about twice the memory required. The matrix dist will be symmetric and will be zero on the diagonal.
#Scipy cdist code#
If you just want the pairwise distance matrix of a single collection A, you can substitute B with A in the code above. This is very efficient, even more so if done in one step as suggests in the comments, dist = np.abs(A - B) The distance between each point of A and each point of B can be calculated as MA = tile(A,A.size)Īnd you'll have for example in dist the distance between the third point of the collection A and fourth point of the collection B. (How do you deal with many dimensions, each of them containing a complex number? for scalars is pretty easy, but for complex you have a few options) The warning is there because pdist and cdist are designed for N-dimensional (scalar) spaces, where such notion of distance does not make any sense. The euclidean norm of a complex number is defined as the modulus of the number, and then you can define the distance between two complex numbers as the modulus of their difference. Is it possible to compute the pairwise distance matrix or the distance between each pair of the two input arrays using cdist or pdist, without using a for loop and which is too slow for my problem? Returns a warning and the distances between the real parts: lib/python2.7/site-packages/scipy/spatial/distance.py:107: ComplexWarning: Casting complex values to real discards the imaginary partĪrray() However the pairwise distance matrix or the distance between each pair of the two input arrays doesn't work: A = (size=(5,1)) + (size=(5,1))*1j Z2 = plex(s(3*numpy.pi/2), numpy.sin(3*numpy.pi/2)) (XA, XB, metric'euclidean', p2, VNone, VINone, wNone) source Computes distance between each pair of the two collections of inputs. The computation of a Euclidean distance between two complex numbers with works: import numpy