MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problem: a matrix and a box...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27271] Re: [mg27268] Problem: a matrix and a box...
  • From: Ken Levasseur <Kenneth_Levasseur at uml.edu>
  • Date: Sun, 18 Feb 2001 02:52:08 -0500 (EST)
  • References: <200102170830.DAA17186@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Bedrosian:

You can use the same trick as is used in cellular automata to get the
"neighbors" of entries in an array:

Here's a "big" array to test thing with:

In[79]:=
big = Array[a, {3, 3}]
Out[79]=
{{a[1, 1], a[1, 2], a[1, 3]}, {a[2, 1], a[2, 2], a[2, 3]}, {a[3, 1], a[3, 2],
a[3, 3]}}


Here is how the list of neighbors can be generated as a matrix of the same
dimensions as the original with the list of  neighbors that you want as the
entries.
In[80]:=
nbs = {#, RotateLeft[#], Transpose[RotateLeft[Transpose[#]]],
          Transpose[RotateLeft[Transpose[RotateLeft[#]]]]} &[big] //
    MapThread[List, #, 2] &
Out[80]=
{{{a[1, 1], a[2, 1], a[1, 2], a[2, 2]}, {a[1, 2], a[2, 2], a[1, 3], a[2, 3]},

   {a[1, 3], a[2, 3], a[1, 1], a[2, 1]}},

  {{a[2, 1], a[3, 1], a[2, 2], a[3, 2]}, {a[2, 2], a[3, 2], a[2, 3], a[3, 3]},

   {a[2, 3], a[3, 3], a[2, 1], a[3, 1]}},

  {{a[3, 1], a[1, 1], a[3, 2], a[1, 2]}, {a[3, 2], a[1, 2], a[3, 3], a[1, 3]},

   {a[3, 3], a[1, 3], a[3, 1], a[1, 1]}}}

Sums can be computed by replacing List with Plus in MapThread.

In[82]:=
NeighborSums[
    big_] := {#, RotateLeft[#], Transpose[RotateLeft[Transpose[#]]],
          Transpose[RotateLeft[Transpose[RotateLeft[#]]]]} &[big] //
    MapThread[Plus, #, 2] &

Here's a numeric example to use as a test.

In[83]:=
test = Partition[Table[Random[Integer, {0, 9}], {16}], 4]
Out[83]=
{{2, 2, 2, 4}, {9, 8, 6, 2}, {4, 2, 6, 3}, {6, 4, 5, 0}}
In[84]:=
NeighborSums[test]
Out[84]=
{{21, 18, 14, 17}, {23, 22, 17, 18}, {16, 17, 14, 13}, {14, 13, 11, 12}}

I'm not sure if this is exactly what you want, but it should be easy enough to
adjust it.

Ken Levasseur
UMass Lowell

Bedrosian Baol wrote:

> Hi
> this is the problem: I have a n x m matrix (with n and m ~3000 elements or
> more)
>
> a1 a2 a3..
> b1 b2 b3..
> c1 c2 c3..
> . . .
> . . .
>
> the algorythm is: compute the summ of the the a1, a2, b1 and b2 elements (a
> ''gliding box'' 2x2), shifing the ''box'' for 1 column and repeat the sum of
> the elements (now a2, a3, b2, b3), and so on...
> at the and of the first line, the box return on the second line and
> repeat...
>
> the final resuls would be another matrix like this:
>
> [a1+a2+b1+b2] [a2+a3+b2+b3]...
> [b1+b2+c1+c2] [b2+b3+c2+c3]...
> ...
>
> I'm not very experienced with Math4: there is a simple and easy way for
> reach
> the result?
>
> thanks
> Bed



  • Prev by Date: Re: matrices & polynomials in mathematica
  • Next by Date: RE: inverting y axis in DensityPlot
  • Previous by thread: Re: Problem: a matrix and a box...
  • Next by thread: Re: Problem: a matrix and a box...