快速r连续匹配(基于位置相似性)

paf*_*paf 6 performance matlab pattern-matching

假设我们有2个相等大小的二进制.

A=101011110000
B=000010101111
Run Code Online (Sandbox Code Playgroud)

我们如何根据类似的位置检查他们的"R"连续匹配?

例如,如果我们设置r = 4,则结果将为假,因为没有4个连续的位置相似性.两个字符串都有0000或1111或1010,但它们不在相似的位置.

但是,如果我们设置:

A=1010111101111
B=1100101011111
Run Code Online (Sandbox Code Playgroud)

结果将为真,因为两个字符串中的最后4个字符(R)等于"1111".

最快的方法是什么?我找到了一个快速解决方案: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
Run Code Online (Sandbox Code Playgroud)

但在此解决方案中,检查相似性不是基于位置.

Div*_*kar 7

IIUC,你可以简单地使用convolution,就像这样 -

any(conv(double(A==B),ones(r,1))>=r)
Run Code Online (Sandbox Code Playgroud)

样品运行

运行#1:

A =
101011110000
B =
000010101111
r =
     4
out =
     0
Run Code Online (Sandbox Code Playgroud)

运行#2:

A =
1010111101111
B =
1100101011111
r =
     4
out =
     1
Run Code Online (Sandbox Code Playgroud)