quickly looping over a 2D array?

Peter Otten __peter__ at web.de
Mon Jul 27 11:12:02 EDT 2009


Martin wrote:

> The statement works now, but it doesn't give the same results as my
> original logic, strangely!?
> 
> in my logic:
> 
> data = np.zeros((numrows, numcols), dtype = np.uint8, order ='C')
> 
> for i in range(numrows):
>     for j in range(numcols):
>         if band3[i,j] == 255 or band3[i,j] >= band6[i,j] * factor:
>             data[i,j] = 0
>         else:
>             data[i,j] = 1
> 
> to do the same with what you suggested...
> 
> data = np.ones((numrows, numcols), dtype = np.uint8, order ='C')
> data[(band3 == 255) | (band6 >= band3 * factor)] = 0

Did you swap band3 and band6? If that's the case, it is an error you should 
be able to find yourself.

Please be a bit more careful.

Also, it is good practice to write a few test cases where you have 
precalculated the result. How would you otherwise know which version is 
correct? Writing a third one to break the tie?

Peter





More information about the Python-list mailing list