[SciPy-user] Centroid Calculation

John Hunter jdhunter at ace.bsd.uchicago.edu
Mon Jan 30 16:57:49 EST 2006


>>>>> "Brian" == Brian Cole <brianc at temple.edu> writes:

    Brian> I'm a NumPy newbie. What is the NumPy way of doing this?
    Brian> n=0 centroid_x=0 centroid_y=0 centroid_z=0 for x, y, z in
    Brian> catesian_coords: centroid_x+=x centroid_y+=y centroid_z+=z
    Brian> n+=1 centroid_x/=n centroid_y/=n centroid_z/=n

where X is a nx3 array of x,y,z,coords

In [11]: import numpy as nx

In [12]: X = nx.rand(1000,3)  # make up some random data

In [13]: centroid = nx.mean(X)

In [14]: print centroid
[ 0.48319355  0.49741983  0.49024469]

JDH




More information about the SciPy-User mailing list