Issue
In python, given one matrix of size 2*3
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 2*100
, how to get all number of distances (i.e. a matrix of size 100*1
easily?
Solution
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])
Answered By - Ivan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.