繁体   English   中英

MATLAB在字符串的单元格数组中找到子字符串数组的重复次数

[英]MATLAB find number of replicates of substring array, in cell array of strings

我有一个字符串的MATLAB单元格数组和一个带有部分字符串的第二个数组:

base = {'a','b','c','d'}
all2 = {'a1','b1','c1','d1','a2','b2','c2','d2','q8','r15'}

输出为:

base = 

    'a'    'b'    'c'    'd'


all2 = 

    'a1'    'b1'    'c1'    'd1'    'a2'    'b2'    'c2'    'd2'    'q8'    'r15'

问题/需求

如果有任何的 'a1' 'b1' 'c1' 'd1'任何 'a2' 'b2' 'c2' 'd2'存在于中all2阵列,然后返回一个可变numb=2

如果有任何的 'a1' 'b1' 'c1' 'd1'任何 'a2' 'b2' 'c2' 'd2' 任何的 'a3' 'b3' 'c3''d3'存在于all2数组中,然后返回变量numb=3

尝试

1。

基于strfind这种方法 ),我尝试过matches = strfind(all2,base); 但是我得到了这个错误:

`Error using strfind`

`Input strings must have one row.`
....

2。

使用strfind 一种方法似乎更好,但只是给了我

fun = @(s)~cellfun('isempty',strfind(all2,s));
out = cellfun(fun,base,'UniformOutput',false)
idx = all(horzcat(out{:}));
idx(1,1) 

out = 

[1x10 logical]    [1x10 logical]    [1x10 logical]    [1x10 logical]


ans =

     0

这些尝试均无济于事。 我认为我的逻辑不正确。

3。

答案允许在字符串数组中找到部分字符串数组的所有索引。 它返回:

base = regexptranslate('escape', base);
matches = false(size(all2));
for k = 1:numel(all2)
    matches(k) = any(~cellfun('isempty', regexp(all2{k}, base)));
end
matches

输出:

matches =

     1     1     1     1     1     1     1     1     0     0

我的方法存在问题:如何使用输出matches计算numb=2 我不确定这是否与我的特定问题最相关,因为它只给出匹配的索引。

在MATLAB中有没有办法做到这一点?

编辑

附加信息:

数组all2将始终是连续的。 all2 = {'a1','b1','c1','d1','a3','b3','c3','d3','q8','r15'}是不可能的。

使用正则表达式查找base元素的唯一后缀:

base = {'a','b','c','d'};
all2 = {'a1','b1','c1','d1','a2','b2','c2','d2', 'a4', 'q8','r15'};

% Use sprintf to build the expression so we can concatenate all the values
% of base into a single string; this is the [c1c2c3] metacharacter.
% Assumes the values of base are going to be one character
%
% This regex looks for one or more digits preceeded by a character from
% base and returns only the digits that match this criteria.
regexstr = sprintf('(?<=[%s])(\\d+)', [base{:}]);

% Use once to eliminate a cell array level
test = regexp(all2, regexstr, 'match', 'once');

% Convert the digits to a double array
digits = str2double(test);

% Return the number of unique digits. With isnan() we can use logical indexing
% to ignore the NaN values
num = numel(unique(digits(~isnan(digits))));

哪个返回:

>> num

num =

     3

如果您需要连续的数字,则类似这样的内容应该是有效的:

base = {'a','b','c','d'};
all2 = {'a1','b1','c1','d1','a2','b2','c2','d2', 'a4', 'q8','r15'};

regexstr = sprintf('(?<=[%s])(\\d+)', [base{:}]);
test = regexp(all2, regexstr, 'match', 'once');
digits = str2double(test);

% Find the unique digits, with isnan() we can use logical indexing to ignore the
% NaN values
unique_digits = unique(digits(~isnan(digits)));

% Because unique returns sorted values, we can use this to find where the
% first difference between digits is greater than 1. Append Inf at the end to
% handle the case where all values are continuous.
num = find(diff([unique_digits Inf]) > 1, 1);  % Thanks @gnovice :)

哪个返回:

>> num

num =

     2

分解regexpsprintf行:因为我们知道base只包含单个字符,所以我们可以使用[c1c2c3]元字符 ,它将匹配方括号内的任何字符。 因此,如果我们使用'[rp]ain'我们将匹配'rain''pain' ,但不会匹配'gain'

base{:}返回MATLAB称为逗号分隔的列表 添加方括号将结果连接到单个字符数组中。

不带括号:

>> base{:}

ans =

    'a'


ans =

    'b'


ans =

    'c'


ans =

    'd'

带括号:

>> [base{:}]

ans =

    'abcd'

我们可以使用sprintf将其插入表达式字符串中。 这样就得到(?<=[abcd])(\\d+) ,它匹配a, b, c, d之一之前的一个或多个数字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM