Mean between positions of two vectors

Asked

Viewed 122 times

1

Guys, how can I average between n vector positions? For example, given an input form vector

 vet = [[10,10,10,10],[10,20,10,10],[10,15,10,10]]

I need the function to return a new vector with the average of the values of each position of each vector, i.e.:

res = [(10+10+10)/3, (10+20+15)/3,...]

The first position of the resulting vector will be the mean of the first positions of the input vector subvectors, the second position of the resulting vector will be the mean of the values of the second positions of the input subvectors and so on!

I am programming in python!

  • In fact what is needed is the average between, for example: average between the first position of the 3 vectors, then the average between the values of the second positions between the vectors, and so on

1 answer

2


If it is guaranteed that all lists have the same number of elements, you can use the function zip to generate the lists with the values of each position, calculating the average by dividing the sum and the amount of elements:

from statistics import mean

res = [mean(values) for values in zip(*vet)]

This will generate the list [10, 15, 10, 10].

  • That’s exactly what I was looking for! Thank you!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.