本文介绍了快速r连续匹配(基于位置相似性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Assume that we have 2 equal size binary.

A=101011110000
B=000010101111

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

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

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

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 .

但是如果我们设置:

A=1010111101111
B=1100101011111

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

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

最快的方法是什么.我在找到了一个快速的解决方案: http://www.mathworks.com/matlabcentral/answers/257051-fast-r-连续匹配

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.

推荐答案

IIUC,您只需使用 convolution ,就像这样-

IIUC, you could simply use convolution, like so -

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


示例运行

运行#1:

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

运行#2:

A =
1010111101111
B =
1100101011111
r =
     4
out =
     1

这篇关于快速r连续匹配(基于位置相似性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:38