Find index of pattern match
17 次查看(过去 30 天)
显示 更早的评论
Hello! I am trying to find index position of a pattern from a string array in a string. for example:
string='Mary had a little lamb';
fcnset=["big","little","small"];
I would want the result as the index of 'little' from the string. I tried the following line, but it doesn't work:
n = find(contains(string,fcnset));
how do I go about it?
0 个评论
采纳的回答
Stephen23
2018-8-13
编辑:Stephen23
2018-8-13
>> str = 'Mary had a little lamb';
>> pat = {'big','little','small'};
>> idx = ~cellfun('isempty',strfind(str,pat))
idx =
0 1 0
>> find(idx)
ans = 2
2 个评论
Stephen23
2018-8-13
编辑:Stephen23
2018-8-13
Aaah, sorry. Try one of these:
>> str = 'Mary had a little lamb';
>> pat = {'big','little','small'};
>> idx = ~cellfun('isempty',regexp(str,pat))
idx =
0 1 0
>> find(idx)
ans = 2
Or
>> str = 'Mary had a little lamb';
>> pat = {'big','little','small'};
>> fun = @(p)isempty(strfind(str,p));
>> idx = ~cellfun(fun,pat)
idx =
0 1 0
>> find(idx)
ans = 2
Or
contains
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!