Compare strings of different dimensions

1 次查看(过去 30 天)
Hi! I have the string s1 and 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'}
I want to compare s1 and its substrings
{'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'}
with s2: I have used strcmp(s1,s2) but I don't obtain the expected result. Can you help me?

采纳的回答

KSSV
KSSV 2016-4-5
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'} ;
[m,n] = size(s2) ;
comp = zeros(size(s2)) ;
for i = 1:m
comp(i,:) = strcmp(s1,s2(i,:)) ;
end
Compare each row of s2 with s1.
  1 个评论
pamela sulis
pamela sulis 2016-4-5
编辑:pamela sulis 2016-4-5
thanks! If I wanto to compare s2 with
s3= {'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'},
how can modify the code?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by