r连续匹配,MATLAB [英] r-contiguous matching, MATLAB
本文介绍了r连续匹配,MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过r连续匹配规则比较这两个字符串. 因此,在此示例中,如果将r设置为6,则它将在第一个示例中返回true,在第二个示例中返回false.
I want to compare these two strings by r-contiguous matching rule. So in this example if we set r as 6 then it will return true for the first example and false for the second example.
示例1:
A='ABCDEFGHIJKLM'
B='XYZ0123EFGHIJAB'
return true (since it they both have 6 contiguous match 'EFGHIJ')
示例2:
A='ABCDEFGHJKLM'
B='XYZ0123EFGHAB'
return false (since they both have only 4 contiguous match 'EFGH')
由于我的数据量巨大,在MATLAB中最快的方法是什么? 谢谢.
What is the fastest way in MATLAB since my data is huge? Thanks.
推荐答案
案例:输入具有唯一字符的字符串
Here's one approach with ismember
& strfind
-
matches = ismember(A,B) %// OR any(bsxfun(@eq,A,B.'),1)
matches_ext = [0 matches 0]
starts = strfind(matches_ext,[0 1])
stops = strfind(matches_ext,[1 0])
interval_lens = stops - starts
out = any(interval_lens >= r)
这里是另一个 diff
& find
而不是strfind
-
matches = ismember(A,B) %// OR any(bsxfun(@eq,A,B.'),1)
matches_ext = [0 matches 0]
df = diff(matches_ext)
interval_lens = find(df == -1) - find(df == 1)
out = any(interval_lens >= r)
这里还有一个 1D convolution
-
Here's another with 1D convolution
-
matches = ismember(A,B) %// OR any(bsxfun(@eq,A,B.'),1)
out = any(conv(double(matches),ones(1,r)) == r)
大小写:输入具有非唯一字符的字符串
这是使用 bsxfun
-
matches = bsxfun(@eq,A,B.'); %//'
intv = (0:r-1)*(size(matches,1)+1)+1
idx = find(matches)
idx = idx(idx <= max(idx) - max(intv))
out = any(all(matches(bsxfun(@plus,idx,intv)),2))
这篇关于r连续匹配,MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文