Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: 2D pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg77276] Re: 2D pattern matching
  • From: "Michael Weyrauch" <michael.weyrauch at gmx.de>
  • Date: Wed, 6 Jun 2007 07:09:45 -0400 (EDT)
  • References: <f43dm5$f8$1@smc.vnet.net>

Hello,

  here is an example to do such things...
m = {{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}}//MatrixForm

o = m /. {a___List, {b___, 1, 1, c___}, {d___, 1, 1, e___}, f___List} /; 
Length[{b}] == Length[{d}] -> 
{a, {b, 2, 2, c}, {d, 2, 2, e}, f}//MatrixForm

The condition on the pattern tries to make sure that you really only replace  2 by 2 submatrices.

Michael



<alexxx.magni at gmail.com> schrieb im Newsbeitrag news:f43dm5$f8$1 at smc.vnet.net...
> 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
> 
>


  • Prev by Date: Re: Two Version 6.0 Package Problems
  • Next by Date: Re: v6: weird cursor behaviour
  • Previous by thread: Re: 2D pattern matching
  • Next by thread: Best practice for Mathematica package development