Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regexp-like library for matrix pattern search

Is there a library (in any language) that can search patterns in matrixes like regular expressions work for strings ? Something like regular expresions for matrixes, or any matrix pattern search method ?

like image 642
clyfe Avatar asked Aug 19 '10 11:08

clyfe


1 Answers

If you're not averse to using J, you can find out whether two matrices are equal by using the -: (match) operator. For example:

   X =: 4 3 $ i.12
   X
0  1  2
3  4  5
6  7  8
9 10 11
   Y =: 4 3 $ (1+i.12)
   Y
 1  2  3
 4  5  6
 7  8  9
10 11 12
   X -: X
1
   X -: Y
0

One nice feature of the match operator is that you can use it to compare arrays of arbitrary dimension; if A is a 3x3x4 array and B is a 2x1 array, then A-:B returns 0.

To find out whether a matrix is a submatrix of another matrix, you can use the E: (member of interval) operator like so:

 X =: 2 2 $ 1 2 4 5  
   X
1 2
4 5
   Y =: 4 3 $ (1+i.12)
   Y
 1  2  3
 4  5  6
 7  8  9
10 11 12
   X E. Y
1 0 0
0 0 0
0 0 0
0 0 0

The 1 at the top left of the result signifies that the part of Y that is equal to X has the given pixel as its upper left-hand corner. The reason for this is that there may be several overlapping copies of X embedded in Y, and only flagging the one pixel lets you see the location of every matching tile.

like image 197
estanford Avatar answered Oct 16 '22 18:10

estanford