[Numpy-discussion] computing average distance
Paul Rudin
paul at rudin.co.uk
Sun Nov 2 14:23:06 EST 2008
I'm experimenting with numpy and I've just written the code below, which
computes the thing I want (I think). Self.bits is an RxRxR array
representing a voxelized 3d model - values are either 0 or 1. I can't
help thinking that's there must be a much nicer way to do it. Any
suggestions?
centre = numpy.array(scipy.ndimage.measurements.center_of_mass(self.bits))
vectors = []
for x in xrange(R):
for y in xrange(R):
for z in xrange(R):
if self.bits[x,y,z]:
vectors.append([x,y,z])
vectors = numpy.array(vectors)
distances = numpy.sqrt(numpy.sum((vectors-centre) ** 2.0, axis=1))
av_dist = numpy.average(distances)
More information about the NumPy-Discussion
mailing list