Home > front end >  taking the norm of 3 vectors in python
taking the norm of 3 vectors in python

Time:11-22

This is probably a stupid question, but for some reason I can't get the norm of three matrices of vectors.

Each vector in the x matrix represents the x coordinate of a sensor (8 sensors total) for three different experiments. Same for y and z.

ex:

x = [array([ 2.239,  3.981, -8.415, 33.895, 48.237, 52.13 , 60.531, 56.74 ]), array([ 2.372,  6.06 , -3.672,  3.704, -5.926, -2.341, 35.667, 62.097])]


y = [array([  18.308,  -17.83 ,  -22.278,  -99.67 , -121.575, -116.794,-123.132, -127.802]), array([  -3.808,    0.974,   -3.14 ,    6.645,    2.531,    7.312, -129.236, -112.   ])]


z = [array([-1054.728, -1054.928, -1054.928, -1058.128, -1058.928, -1058.928, -1058.928, -1058.928]), array([-1054.559, -1054.559, -1054.559, -1054.559, -1054.559, -1054.559, -1057.959, -1058.059])]

I tried doing:

norm= np.sqrt(np.square(x) np.square(y) np.square(z))
x = x/norm
y = y/norm
z = z/norm

However, I'm pretty sure its wrong. When I then try and sum the components of let's say np.sum(x[0]) I don't get anywhere close to 1.

CodePudding user response:

Normalization does not make the sum of the components equal to one. Normalization makes the norm of the vector equal to one. You can check if your code worked by taking the norm (square root of the sum of the squared elements) of the normalized vector. That should equal 1.

From what I can tell, your code is working as intended.

I made a mistake - your code is working as intended, but not for your application. You could define a function to normalize any vector that you pass to it, much as you did in your program as follows:

def normalize(vector):
    norm = np.sqrt(np.sum(np.square(vector)))
    return vector/norm

However, because x, y, and z each have 8 elements, you can't normalize x with the components from x, y, and z.

What I think you want to do is normalize the vector (x,y,z) for each of your 8 sensors. So, you should pass 8 vectors, (one for each sensor) into the normalize function I defined above. This might look something like this:

normalized_vectors = []
for i in range(8):
    vector = np.asarray([x[i], y[i],z[i]])
    normalized_vectors.append = normalize(vector)
  • Related