I want to compare 2 lists. One is very large and I need to compare many times.
Lets say Length[list1] < Length[list2].
I need to know how many times list1 occures in list2.
list1 = {1, 0}
list2 = {1, 0, 1, 0, 1, 1}
So the result would be 2 (at position 1 and 3).
Furthermore list1 can countain wildcards
list1 = {1, 2}
Where 2 is a wildcard, so with list2 from above the result would be 3 (at position 1,3,5).
I solved this with a few For loops. It works but is really slow. I need to speed it up very much.
What I got:
With list1 as lMask and list2 as BitData
GetFits[i_] := Block[{icount, lMask},
icount = 0;
lMask = IntegerDigits[i, 3];
If[lMask[[-1]] != 2 ,
If[ lMask[[1]] != 2,
For[ii = 1, ii <= Length[BitData] + 1 - Length[lMask], ii++,
If[FitAt[lMask, ii] == 1, icount++;];
];
icount
, -1]
, -1]
]
FitAt[lMask_, iPos_] := (For[i = 1, i <= Length[lMask], i++,
If[lMask[[i]] != 2,
If[lMask[[i]] != BitData[[i + iPos - 1]],
Return[0]
];
];
];
1)
seqPosandseqPosCin my answer there), and here. – Leonid Shifrin Nov 07 '14 at 18:12