quickly looping over a 2D array?

Martin mdekauwe at gmail.com
Mon Jul 27 12:00:41 EDT 2009


On Jul 27, 4:12 pm, Peter Otten <__pete... at web.de> wrote:
> 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

apologies... it was the way I typed it up, I wasn't copying and
pasting from my text editor!
Thanks Peter.



More information about the Python-list mailing list