I am struggling to vectorize the following operation. I have an array of x,y,z distances and I need to find the differences between each vector from one another.
temp_result = np.array([[0.8, 0., 1.], [0., -0.6, 1.],[0.8, 0., 1.]])
What I intend to do is subtract without using for loop iteration.
temp_result[0] - temp_result[0]
temp_result[0] - temp_result[1]
temp_result[0] - temp_result[2]
temp_result[1] - temp_result[0]
temp_result[1] - temp_result[1]
temp_result[1] - temp_result[2]
temp_result[2] - temp_result[0]
temp_result[2] - temp_result[1]
temp_result[2] - temp_result[2]
thanks!
Here's a nice reshape-based trick:
arr = temp_result
diffs = arr[:,None,:] - arr[None,:,:]
Then the vector difference between arr[i]
and arr[j]
is found in diffs[i,j]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With