MathGroup Archive 2007

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

Search the Archive

Re: 2D pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg77264] Re: [mg77140] 2D pattern matching
  • From: János <janos.lobb at yale.edu>
  • Date: Wed, 6 Jun 2007 07:03:31 -0400 (EDT)
  • References: <200706051024.GAA00465@smc.vnet.net>

On Jun 5, 2007, at 6:24 AM, alexxx.magni at gmail.com wrote:

> hi everybody,
>
> do you know how to do a pattern match involving 2D matrices?
> If e.g. you have a list
>
> a = {{0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 1, 0, 1, 1, 0}, {0,
>    0, 0, 1, 1, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}}
>
> i.e.
> 0 0 0 0 0 0
> 0 1 0 0 0 0
> 0 1 0 1 1 0
> 0 0 0 1 1 0
> 0 0 0 0 0 0
>
> is it possible for me to write a rule by which I am able to find the
> 2x2 square of 1's, and replace it with something else?
>
> I studied all I could on patterns, but didnt find an answer...
>
> thanks for ANY help
>
> Alessandro Magni
>

Here is a newbie bit-blow-up approach:

In[2]:=
b = Partition[a, {2, 2},
    {1, 1}]
Out[2]=
{{{{0, 0}, {0, 1}},
    {{0, 0}, {1, 0}},
    {{0, 0}, {0, 0}},
    {{0, 0}, {0, 0}},
    {{0, 0}, {0, 0}}},
   {{{0, 1}, {0, 1}},
    {{1, 0}, {1, 0}},
    {{0, 0}, {0, 1}},
    {{0, 0}, {1, 1}},
    {{0, 0}, {1, 0}}},
   {{{0, 1}, {0, 0}},
    {{1, 0}, {0, 0}},
    {{0, 1}, {0, 1}},
    {{1, 1}, {1, 1}},
    {{1, 0}, {1, 0}}},
   {{{0, 0}, {0, 0}},
    {{0, 0}, {0, 0}},
    {{0, 1}, {0, 0}},
    {{1, 1}, {0, 0}},
    {{1, 0}, {0, 0}}},
   {{{0, 0}, {0, 0}},
    {{0, 0}, {0, 0}},
    {{0, 0}, {0, 0}},
    {{0, 0}, {0, 0}},
    {{0, 0}, {0, 0}}}}

/If you do a MatrixForm on b you will able to see where that block 
is  and how you should get the staring coordinates:)/

Then

In[4]:=
Position[b, {{1, 1}, {1, 1}}]
Out[4]=
{{3, 4}}

gives the upper left position of the block in b and that is the same 
as in a.  /I leave the proof of it to the experts :)/

Replacing now in a is easy because you know how many cells in right 
and down do you want to replace from the found starting position.


J=E1nos


---------------------------------------------
"Its like giving a glass of ice water to somebody in Hell"
Steve Jobs about iTunes on Windows




  • Prev by Date: Re: Segregating the elements of a list based on given lower and upper bounds
  • Next by Date: Re: RE: RE: simple question
  • Previous by thread: 2D pattern matching
  • Next by thread: Re: Re: 2D pattern matching