[Numpy-discussion] computing average distance

Paul Rudin paul@rudin.co...
Sun Nov 2 13:23:06 CST 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

 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 = 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