``````def CalcCentre(data):
centre = array([0,0,0])
count = 0
for p in data:
count += 1
centre = dot(1./count, centre)
return centre
``````
4
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by Inshu

pretty useless until you give more codes to support that. sorry ..

Code use Numpy and it use function add() and dot().
To see what these numpy function dos,look into numpy doc here is add()
To test function so it work i guessed for a nested list as input data.

``````from numpy import *

def CalcCentre(data):
centre = array([0,0,0])
count = 0
for p in data:
count += 1
centre = dot(1./count, centre)
return centre

print CalcCentre(array([[1,2,3], [4,5,6]]))
#--> [ 2.5  3.5  4.5]
``````

Edited by snippsat

well spotted

So the function calculates the barycentre of a set of points in a multidimensional space.

``````import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin

def CalcCentre(data):
centre = array([0,0,0])
count = 0
n = 0
#data = list(data)
for p in data[n:n+3]:
count += 1
centre = dot(1./count, centre)
return centre
n += 1
f = open(filename)
result = []
for l in f:
sp = l.split()
t = tuple(map(float, sp[1:4]))
result.append(t)
return result

def main (data):

j = 0
for  i in len (data):
while j != 3:
centre = CalcCentre(data)
j += 1
print centre

if __name__ == '__main__':

main(data)
``````

Edited by Inshu

now this is the full prog or code..i want to find the centroid of every three points in a 3D data point cloud... but the code is printing the centroid for whole data 3 times...

also i want to know what does 'dot() ' function do ?? Also why len(data) is not working in the code ??

Edited by Inshu

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.