Skip to main content
`Ctrl`+`K`
scipy.spatial.

# distance_matrix

scipy.spatial.distance_matrix(x, y, p=2.0, threshold=1000000)

  Compute the distance matrix.

  Returns the matrix of all pair-wise distances.

  Deprecated since version 1.18.0: This function is deprecated in favor of `scipy.spatial.distance.cdist` and will be removed in SciPy 1.20.0.

  Parameters:
    x(M, K) array_like

      Matrix of M vectors in K dimensions.

    y(N, K) array_like

      Matrix of N vectors in K dimensions.

    p float, 1 <= p <= infinity

      Which Minkowski p-norm to use.

    threshold positive int

      If `M * N * K` > threshold, algorithm uses a Python loop instead of large temporary arrays.

  Returns:
    result(M, N) ndarray

      Matrix containing the distance from every vector in x to every vector in y.

  Notes

  Array API Standard Support

  `distance_matrix` is not in-scope for support of Python Array API Standard compatible backends other than NumPy.

  See Support for the array API standard for more information.

  Examples

      >>> from scipy.spatial import distance_matrix
      >>> distance_matrix([[0,0],[0,1]], [[1,0],[1,1]])
      array([[ 1.        ,  1.41421356],
             [ 1.41421356,  1.        ]])