Numpy Broadcast to perform euclidean distance vectorized

后端 未结 5 2125
无人共我
无人共我 2020-12-04 14:44

I have matrices that are 2 x 4 and 3 x 4. I want to find the euclidean distance across rows, and get a 2 x 3 matrix at the end. Here is the code with one for loop that compu

5条回答
  •  忘掉有多难
    2020-12-04 15:30

    Simply use np.newaxis at the right place:

     np.sqrt((np.square(a[:,np.newaxis]-b).sum(axis=2)))
    

提交回复
热议问题