[Numpy-discussion] Rasterizing points onto an array

Thomas Robitaille thomas.robitaille@gmail....
Sun May 31 19:39:06 CDT 2009


I have a set of n points with real coordinates between 0 and 1, given  
by two numpy arrays x and y, with a value at each point represented by  
a third array z. I am trying to then rasterize the points onto a grid  
of size npix*npix. So I can start by converting x and y to integer  
pixel coordinates ix and iy. But my question is, is there an efficient  
way to add z[i] to the pixel given by (xi[i],yi[i])? Below is what I  
am doing at the moment, but the for loop becomes very inefficient for  
large n. I would imagine that there is a way to do this without using  
a loop?


import numpy as np

n = 10000000
x = np.random.random(n)
y = np.random.random(n)
z = np.random.random(n)

npix = 100
ix = np.array(x*float(npix),int)
iy = np.array(y*float(npix),int)

image = np.zeros((npix,npix))
for i in range(len(ix)):
     image[ix[i],iy[i]] = image[ix[i],iy[i]] + z[i]


Thanks for any advice,


More information about the Numpy-discussion mailing list