Re: Please Help
- To: mathgroup at smc.vnet.net
- Subject: [mg4143] Re: Please Help
- From: gaylord at ux1.cso.uiuc.edu (richard j. gaylord)
- Date: Fri, 7 Jun 1996 02:08:01 -0400
- Organization: university of illinois
- Sender: owner-wri-mathgroup at wolfram.com
In article <4ou1ql$7tp at dragonfly.wolfram.com>, lou at wolfram.com (Lou D'Andria) wrote: In[3]:= NeighborhoodMaxes[ mat_?(MatrixQ[#,Positive]&) ] := Apply[Max, Partition[#,{3,3},{1,1}], {2}]& @ Join[{#},Join[{0},#,{0}]& /@ mat,{#}]& @ Table[{0},{2 + Length[First[mat]]}] >you can > use Partition to get the neighborhoods, and Max to find the maximum in > each neighborhood. > I can't take credit for discovering this Partition method of extracting > neighborhoods, but I no longer remember who first showed it to me. this method is used in the Life.m notebook that came with with mathematica 1.0 (and each newer version as well, though i don't know why since it is not a very good mathematica program). unfortunately it is a SLOOOW way to work with neighborhoods. here is a way which is faster [and the speed difference increases with the lattice size]. note: the following is taken from the book "Modleing Nature: Cellular Automata Simulationsd with Mathematica" coming out in about six weeks. In[1]:= Moore[func__, lat_] := MapThread[func, Map[RotateRight[lat, #]&, {{0, 0}, {1, 0}, {0, -1}, {-1, 0}, {0, 1}, {1, -1}, {-1, -1}, {-1, 1}, {1, 1}}], 2] In[2]:= absorbBC = (Prepend[Append[Map[Prepend[Append[#, 0], 0]&, #], Table[0, {Dimensions[#][[2]] + 2}]], Table[0, {Dimensions[#][[2]]+ 2}]])&; In[24]:= Map[Take[#, {2, -2}]&, Take[Moore[Max, absorbBC[lat]], {2, -2}]] timing comaprision [on a powermac 8500/120] In[4]:= lat = Table[ Random[Integer,{1,9}], {100},{150}]; In[7]:= Timing[dog = Map[Take[#, {2, -2}]&, Take[Moore[Max, absorbBC[lat]], {2, -2}]];] Out[7]= {1.68333 Second, Null} In[8]:= Timing[cat = NeighborhoodMaxes[lat];] Out[8]= {2.65 Second, Null} In[9]:= dog == cat Out[9]= True In[11]:= Timing[dog = Map[Take[#, {2, -2}]&, Take[Moore[Max, absorbBC[lat]], {2, -2}]];] Out[11]= {3.03333 Second, Null} In[12]:= Timing[cat = NeighborhoodMaxes[lat];] Out[12]= {4.18333 Second, Null} In[13]:= dog == cat Out[13]= True moral of the story: never dissasemble a matrix and the reassemble it later. use mathematica's apl-like ability to manipulate entire matrices in order to feed neighborhood values into whatever function you want to apply to the neighborhoods. -- "if you're not programming functionally, then you're programming dysfunctionally" ==== [MESSAGE SEPARATOR] ====