Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast r-contiguous matching (based on location similarities)

Assume that we have 2 equal size binary.

A=101011110000
B=000010101111

How can we check their "R" contiguous matching based on similar location?

For example if we set r=4 then the result will be false since there is no 4 contiguous similarities of locations. Both strings have 0000 or 1111 or 1010 but they are not in similar location .

However if we set :

A=1010111101111
B=1100101011111

The result will be true since the last 4 char (R) in both strings are equal to "1111".

What is the fastest way to do that. I found a fast solution in : http://www.mathworks.com/matlabcentral/answers/257051-fast-r-contiguous-matching

bin = 2.^(0:r - 1);
A2 = filter(bin, 1, A == '1');
B2 = filter(bin, 1, B == '1');
bool = any(ismember(A2(r:end), B2(r:end))); % need to trim first r-1 entries

But in this solution checking similarities is not based on location.

like image 238
pafpaf Avatar asked Dec 08 '15 10:12

pafpaf


1 Answers

IIUC, you could simply use convolution, like so -

any(conv(double(A==B),ones(r,1))>=r)

Sample runs

Run #1 :

A =
101011110000
B =
000010101111
r =
     4
out =
     0

Run #2 :

A =
1010111101111
B =
1100101011111
r =
     4
out =
     1
like image 116
Divakar Avatar answered Nov 04 '22 15:11

Divakar