So I am trying to vectorize this code in numpy and im having trouble Heres is my for loop version which works as desired:
B is a 3x3 Matrix
for i in range(b.shape[0]):
for j in range(b.shape[0]):
Z[i,j] = np.sqrt(np.dot((b[i,:].T - b[j,:].T).T , b[i,:].T - b[j,:].T))
Now I am trying to vectorized this code so I dont have to use a double for loop. Here what I have gotten so far which does not work:
i = 0, j = 0
np.sqrt( np.dot ( (p[i,:].T - p[j:,:].T).T , p[i,:].T - p[j,:].T ))
Ideally it should perform this if you break down to what a for loop would do.
np.sqrt( np.dot ( (p[0,:].T - p[0,:].T).T , p[0,:].T - p[0,:].T ))
np.sqrt( np.dot ( (p[0,:].T - p[1,:].T).T , p[0,:].T - p[1,:].T ))
np.sqrt( np.dot ( (p[1,:].T - p[0,:].T).T , p[1,:].T - p[0,:].T ))
np.sqrt( np.dot ( (p[1,:].T - p[1,:].T).T , p[1,:].T - p[1,:].T ))
Can someone kindly provide some insight. I would prefer not to use any built in functions and stick to using things like np.dot. Btw this is to compute the Eucledean distance matrix.