[SciPy-user] Loopless square 2d arrays with radially symmetric functions
josef.pktd@gmai...
josef.pktd@gmai...
Wed Mar 25 10:22:46 CDT 2009
On Wed, Mar 25, 2009 at 10:58 AM, David Vine <djvine@gmail.com> wrote:
> Hello,
>
> I use a lot of radially symmetric arrays in my code and I would like to know
> if there is a more efficient method for creating these arrays than the
> nested for-loops i am currently using.
>
> For example, to create a Gaussian I would currently do this (Python 2.5,
> Ubuntu Intrepid):
> import scipy
> p = scipy.zeros((256,256))
> for i in xrange(256):
> for j in xrange(256):
> p[i,j] = exp(-0.01*( (i-128.)**2. - (j-128.)**2.) )
>
> and my question is whether there is a more efficient 'loopless' method (i.e
> involving no for-loops) ?
>
> Thanks in advance
> David
>
I think this does the same with broadcasting
import numpy as np
i = np.arange(256)
p = np.exp(-0.01*( (i[:,np.newaxis]-128.)**2. - (i[np.newaxis,:]-128.)**2.))
Josef
More information about the SciPy-user
mailing list