python - fill Numpy array with axisymmetric values -


i'm trying find fast way fill numpy array rotation symmetric values. imagine array of zeros containing cone shaped area. have 1d array of values , want rotate 360° around center of array. there no 2d function z=f(x,y), can't calculate 2d values explicitly. have works, for-loop slow big arrays. should make circle:

values = np.ones(100) x = np.arange(values.size)-values.size/2+0.5 y = values.size/2-0.5-np.arange(values.size) x,y = np.meshgrid(x,y) grid = np.rint(np.sqrt(x**2+y**2)) arr = np.zeros_like(grid) in np.arange(values.size/2):     arr[grid==i] = values[i+values.size/2] 

my 1d array of course not simple. can think of way rid of for-loop?

update: want make circular filter convolutional blurring. before used np.outer(values,values) gave me rectangular filter. david's hint allows me create circular filter fast. see below:

square filter np.outer()

circular filter david's answer

you can use fancy indexing achieve this:

values = np.ones(100) x = np.arange(values.size)-values.size/2+0.5 y = values.size/2-0.5-np.arange(values.size) x,y = np.meshgrid(x,y) grid = np.rint(np.sqrt(x**2+y**2)).astype(np.int)  arr = np.zeros_like(grid) size_half = values.size // 2 inside = (grid < size_half) arr[inside] = values[grid[inside] + size_half] 

here, inside select indices lie inside circle, since these items can derived values.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -