Jose Gracia Rodriguez
Jose Gracia Rodriguez

Reputation: 167

How to subtrac a numpy array element-by-elemnt by another numpy array

I have two numpy arrays, with just the 3-dimensional coordinates of two molecules.

I need to implement the following equation, and I'm having problems in the subtraction of each coordinate of one of the arrays by the second, and then square it.

enter image description here

I have tried the following, but since I'm still learning I feel that I am making some major mistake. The simple code I use is:

a = [math.sqrt(1/3*((i[:,0]-j[:,0])**2) + ((i[:,1] - j[:,1])**2) + ((i[:,2]-j[:,2])**2) for i, j in zip(coordenates_2, coordenates_1))]

Upvotes: 1

Views: 123

Answers (1)

David
David

Reputation: 8298

It's numpy you can easily do it using the following example:

import numpy as np
x1 = np.random.randn(3,3,3)
x2 = np.random.randn(3,3,3)

res = np.sqrt(np.mean(np.power(x1-x2,2)))

Upvotes: 1

Related Questions