Improving style (and speed)?
Alain TESIO
alain at onesite.org
Thu Aug 3 16:50:50 EDT 2000
Hello,
I don't know how the Numeric module is working, if I'm guessing right
this code should work.
>From an almost newbie !
Alain
Miny=Posy-1
if Miny<0:
Miny=0
Maxy=Posy+2
Minz=Posz-1
if Minz<0:
Minz=0
Maxz=Posz+2
mincoord=[]
#your lower bound:
minvalue=-99999
for y in range(miny,maxy+1):
for z in range(minz,maxz+1):
value=Array3D[posx,y,z]
#replace with abs(value-minvalue)<espilon if it's an
array of floats:
if value=minvalue:
mincoord.append([posx,y,z])
elif value<minvalue:
mincoord=[[x,y,z]]
minvalue=value
(posx,posy,posz)=mincoord[whrandom.randint(0,len(mincoord)-1)]
On Thu, 03 Aug 2000 19:23:43 GMT, ingoogni at home.nl (ingo) wrote:
>Dear all,
>A week or two ago I started learning programming and Python. Did a few small
>things so far and they work :) Trying something bigger now; manipulating a
>3D-dataset. There's one part in it, although it works, I don't like the way
>I did it. But I can't find a better, more elegant, way to do it. Lack of
>experience. So I'm looking for some guidance.
>
>What I'm doing is select a small (3x3) region in the dataset. Within this
>selection find the minimum value and its indices in the 3d-dataset . If
>there are more minima select one randomly. I'm not yet taking into account
>that the selection also can be 2x2, 2x3 or 3x2 when it's on one of te edges
>of the dataset.
>
>Coming from "Array3d[Posx, Posy, Posz]"
>
>Miny=Posy-1
>if Miny<0:
> Miny=0
>Maxy=Posy+2
>Minz=Posz-1
>if Minz<0:
> Minz=0
>Maxz=Posz+2
>
>Nine=Numeric.ravel(Array3d[Posx, Miny:Maxy, Minz:Maxz])
>MinNine=min(Nine)
>EqMin=Numeric.equal(Nine,MinNine)
>MinIndex=Numeric.nonzero(EqMin)
>Index=whrandom.randint(0,len(MinIndice)-1)
>MinIndex=MinIndex[Index]
>
>if MinIndex==0:
> Posy=Posy-1
> Posz=Posz-1
>elif MinIndex==1:
> Posy=Posy
> Posz=Posz-1
>elif MinIndex==2:
> Posy=Posy+1
> Posz=Posz-1
>elif MinIndex==3:
> Posy=Posy-1
> Posz=Posz
>elif MinIndex==4:
> Posy=Posy
> Posz=Posz
>elif MinIndex==5:
> Posy=Posy+1
> Posz=Posz
>elif MinIndex==6:
> Posy=Posy+1
> Posz=Posz-1
>elif MinIndex==7:
> Posy=Posy+1
> Posz=Posz
>elif MinIndex==8:
> Posy=Posy+1
> Posz=Posz+1
>
>now do something with "Array3d[Posx, Posy, Posz]"
>
>TIA,
>Ingo
>
>--
>Photography: http://members.home.nl/ingoogni/
>Pov-Ray : http://members.home.nl/seed7/
More information about the Python-list
mailing list