Find index of pattern match
24 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Stephen23
on 13 Aug 2018
Edited: Stephen23
on 13 Aug 2018
>> 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 Comments
Stephen23
on 13 Aug 2018
Edited: Stephen23
on 13 Aug 2018
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
See Also
Categories
Find more on Linear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!