Skip to content
Advertisement

Get Centroid from Sequence in N-Dimensions

I am trying to get the centroid for a series in n-dimensions.

This is what I have tried:

JavaScript

But what I am trying to get is:

JavaScript

What am I missing here?

Thanks in advance.

Advertisement

Answer

Use list comprehension instead of sum function because:

  1. sum returns the sum of all elements of an iterable, so it is now returning the sum of the averages
  2. Instead you need average of the corresponding values in each list/points
  3. List comprehension creates a list of values, each value given by the formula you have already written for find the average

Try the following:

Code

JavaScript

Output

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement