Home > Net >  How to compute distance for a matrix and a vector?
How to compute distance for a matrix and a vector?

Time:09-21

In python, given one matrix of size 3*2 like A=[[x11,x12,x13],[x21,x22,x23]] and a column vector b=[mu1;mu2]. If I want to compute the Euclidean distance between each column of A and vector b. For example, for the first column, the distance 'd1` is given by

A=[[x11,x12,x13],[x21,x22,x23]]
b=[[mu1],[mu2]]
d1=(x11-mu1)^2 (x21-mu2)^2
#second column
d2=(x12-mu1)^2 (x22-mu2)^2
# so on

So the distance can be scored in a matrix [d1,d2,d3] of size 3*1:

I can compute the distance for a smaller matrix. But if matrix A is larger like size 100*2, how to get all number of distances (i.e. a matrix of size 100*1 easily?

CodePudding user response:

You can use point-wise substraction, then square and sum the two columns:

>>> np.sum((A-b)**2, 1)

As noted by @Gilad Green, if b is shaped (n, 1) then a transpose will be required:

Either on b:

>>> np.sum((A-b.T)**2, 1)

Or on A (notice the summation on axis=0)

>>> np.sum((A.T-b)**2, 0)

>>> A = np.random.rand(3,2)
>>> b = np.random.rand(2)

>>> A - b
array([[0.21263611, 0.47988496],
       [0.91061396, 0.93371001],
       [0.66321026, 0.21926392]])

>>> (A - b)**2
array([[0.04521411, 0.23028957],
       [0.82921779, 0.87181438],
       [0.43984785, 0.04807667]])

>>> np.sum((A-b)**2, 1)
array([0.27550369, 1.70103217, 0.48792451])

CodePudding user response:

For Euclidean distance, you can use any of the following methods:

In [1]: import numpy as np

In [2]: A = np.random.rand(3, 2)

In [3]: b = np.random.rand(2)

In [4]: Ab = A - b

In [5]: Ab.shape
Out[5]: (3, 2)

In [6]: A
Out[6]: 
array([[0.88888598, 0.71558933],
       [0.42653942, 0.80202214],
       [0.37195937, 0.24403248]])

In [7]: b
Out[7]: array([0.13348342, 0.91007169])

In [8]: Ab
Out[8]: 
array([[ 0.75540256, -0.19448237],
       [ 0.293056  , -0.10804955],
       [ 0.23847595, -0.66603921]])

In [9]: np.sqrt(np.sum(Ab**2, axis=1))
Out[9]: array([0.78003617, 0.3123404 , 0.70744541])

In [10]: np.linalg.norm(Ab, axis=1)
Out[10]: array([0.78003617, 0.3123404 , 0.70744541])
  • Related