Re: Re: 2D pattern matching

• To: mathgroup at smc.vnet.net
• Subject: [mg77437] Re: [mg77264] Re: [mg77140] 2D pattern matching
• From: János <janos.lobb at yale.edu>
• Date: Fri, 8 Jun 2007 05:41:02 -0400 (EDT)
• References: <200706051024.GAA00465@smc.vnet.net> <3789605.1181136409894.JavaMail.root@m35> <op.ttilxgo2qu6oor@monster.ma.dl.cox.net>

```Of course if he is changing that 2x2 with a nxm where either n or m
is not 2, then of course he will be in trouble if he wants to do the
change for every 2.2 at once.

As a newbie I had just 2x2-s in my mind :)

Thanks,

J=E1nos
P.S.  What has happened to DrBob ?  Did he go to the Army ? :)

On Jun 6, 2007, at 3:33 PM, DrMajorBob wrote:

> That solution indicates, initially, the need for an inverse to
> Partition[x_,{2,2},{1,1}] and similar things.
>
> After changing one of the 2x2's, though, the "blown-up" matrix
> isn't in the domain of that inverse.
>
> Bobby
>
> On Wed, 06 Jun 2007 06:03:31 -0500, J=E1nos <janos.lobb at yale.edu> =
wrote:
>
>>
>> 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
>>
>>
>>
>>
>
>
>
> --
> DrMajorBob at bigfoot.com

```

• Prev by Date: Graphics3D exported to pdf
• Next by Date: Re: PolyLogs
• Previous by thread: Re: Re: 2D pattern matching
• Next by thread: Re: 2D pattern matching