我有字符串s1和s2
s1={'1' '631' '618' '574' '678'}
s2={'1' '596' '674' '' '';'674' '631' '1' '631' '1';'641' '617' '674' '631' '654';'674' '673' '674' '673' '674';'674' '618' '1' '618' '631';'631' '1' '631' '674' '740';'739' '740' '733' '674' '631';'674' '673' '674' '1' '641';'618' '1' '631' '618' '631';'674' '631' '618' '631' '618';'674' '631' '1' '631' '625';'641' '642' '618' '631' '618';'618' '631' '1' '631' '1'}我想比较一下s1和它的子字符串
{'1'}
{'1' '631'}
{'1' '631' '618'}
{'1' '631' '618' '574'}
{'1' '631' '618' '574' '678'}
{'631'}
{'631' '618'}
{'631' '618' '574'}
{'631' '618' '574' '678'}
{'618'}
{'618' '574'}
{'618' '574' '678'}
{'574'}
{'574' '678'}
{'678'} 使用s2:我使用了strcmp(s1,s2),但没有得到预期的结果。你能帮帮我吗?
发布于 2016-04-05 11:14:43
我强烈建议将所有字符串转换为数字,并使用矩阵操作而不是字符串操作:
S1 = cellfun(@str2num, s1)
S2 = cell2mat(str2double (s2)) %// NOTE its str2double here which converts any empty string or char into a NaN现在做比较,如果你想要相交(我认为你是)
[intersect ind] = ismember(S2,S1);如果您想坚持使用String,您可以这样做,这样做效率要高得多:
ind=find(ismember(s2,s1{1}))
>> ind =
1
19
22
28
31
37
39
47
54
65strcmp的问题是,它比较两个字符串并返回一个逻辑,在您的示例中,您将面临5*65操作,这在一般情况下是耗时和糟糕的。因此,ismember函数是您的最佳选择。
要生成"s1及其子字符串“,可以使用combnk,如:
V = combnk(S1,1)
V = combnk(S1,2) %//change 1 to 5 based on the combinations.https://stackoverflow.com/questions/36424108
复制相似问题