       • To: mathgroup at smc.vnet.net
• 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:=
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:=
Moore[func__, lat_] :=
Map[RotateRight[lat, #]&,
{{0, 0}, {1, 0}, {0, -1}, {-1, 0}, {0, 1},
{1,  -1}, {-1, -1}, {-1, 1}, {1, 1}}], 2]

In:=
absorbBC = (Prepend[Append[Map[Prepend[Append[#, 0], 0]&, #],
Table[0, {Dimensions[#][] + 2}]],
Table[0, {Dimensions[#][]+ 2}]])&;

In:=
Map[Take[#, {2, -2}]&, Take[Moore[Max, absorbBC[lat]], {2, -2}]]

timing comaprision [on a powermac 8500/120]

In:=
lat = Table[ Random[Integer,{1,9}], {100},{150}];

In:=
Timing[dog = Map[Take[#, {2, -2}]&, Take[Moore[Max, absorbBC[lat]], {2, -2}]];]
Out=
{1.68333 Second, Null}

In:=
Timing[cat = NeighborhoodMaxes[lat];]
Out=
{2.65 Second, Null}

In:=
dog == cat
Out=
True

In:=
Timing[dog = Map[Take[#, {2, -2}]&, Take[Moore[Max, absorbBC[lat]], {2, -2}]];]
Out=
{3.03333 Second, Null}

In:=
Timing[cat = NeighborhoodMaxes[lat];]
Out=
{4.18333 Second, Null}

In:=
dog == cat
Out=
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] ====

```

• Prev by Date: MathLink questions
• Next by Date: Kei Ker from Bessel functions