0
$\begingroup$

I have two matrix V_r of shape(19, 300) and vecs of shape(100000, 300). I would like to subtract rows of V_r from from rows of vecs. Currently, I am achieving this with the following code. Is there a way to do it using broadcasting?

    list=[]
for v in V_r:
    a=np.linalg.norm(vecs-v,axis=1)
    list.append(a)
M=np.vstack(list)
$\endgroup$

2 Answers 2

0
$\begingroup$

This is what you're looking for : np.linalg.norm(vecs-V_r[:,np.newaxis], axis = 2)

$\endgroup$
0
$\begingroup$

This worked for me:

np.linalg.norm(vecs[:, :, None] - V_r[None, :, :].transpose(0, 2, 1), axis=1)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.