What is the best method to search patterns for 2D list ? Let L be following 2D-list :
{{0,1,0,1,0,1,1},
{1,0,1,0,1,1,0},
{1,1,1,0,1,0,0},
{1,0,1,1,0,1,1},
{0,0,1,0,1,1,1},
{1,0,1,1,1,0,1},
{1,0,0,1,1,0,1}}
0 1 0 1 0 1 1
1 0 1 0 1 1 0
1 1 1 0 1 0 0
1 0 1 1 0 1 1
0 0 1 0 1 1 1
1 0 1 1 1 0 1
1 0 0 1 1 0 1
We want to find positions of pattern
1 ?
1 1
Check it screenshot:
find positions of following pattern :
All 0,1 are related,
Which structure is the best (fast, convinient, widely-used) to do such job?
I've found SparseArray
, ArrayPlot
, Grid
are promising. Also just List
(nested list) can be good.
Another candidate is creating a function f
and f[1,1]=0, f[1,2]=1,..,f[7,7]=1
Also image based technique can be considered.
For now, I am satisfied with the following condition :
Position[ListCorrelate[{{1, 0}, {1, 1}}, A], 3]
orSparseArray[ UnitStep[ ListCorrelate[{{1, 0}, {1, 1}}, A] - 3]]["NonzeroPositions"]
, whereA
is your input array. $\endgroup$ListCorrelate
, it uses multiplication and addition. Can this applied to find more complicated fixed pattern of 0s and 1s ? $\endgroup$