I have a cell 106x1
of strings TrajCompact
. In every string I want to find all the prefix that are combination of (0,1,2,3,4)
and consider only the postfix after this prefix My prefix are:
00,01,02,03,04,10,11,12,13,14,20,21,22,23,24,30,31,32,33,34,40,41,42,43,44
I try to do this, using the code
for k=1:size(TrajCompact,1)
matches(k) = regexp(TrajCompact(k), '\(?00.*', 'match', 'once');
end
The code run but I have to write it 25 times: one for every prefix. I want to find a compact expression so I modify the code in this way:
[digits{1:2}] = ndgrid(0:4);
for k=1:106
matches(k) = regexp(TrajCompact(k), sprintf('?%d%d.*', digits{1}(k), digits{2}(k)), 'match', 'once');
end
but it doesn't make what I want, what is the flaw here?
You should solve the problem inside the regexp
:
matches{k} = regexp(TrajCompact{k}, '\(?[0-4][0-4].*', 'match', 'once');
Later Edit
Based on a longer discussion in the comments of this post, the solution for OPs problem should lie along the lines of using the function:
get_matches = @(x,c)cellfun(@(s)regexp(s,sprintf('\\(%s\\)[0-4]+',x),'match'),c,'UniformOutput',false);
For example, for the string cell:
str_cell = {'(23)2' '02(13)(23)1(23)21(23)20(23)1(23)21(23)1(23)(13)(23)1(32)31(32)12321(23)21321(23)132(31)(32)31(32)12321(23)21(23)21(23)20(23)2' '210432342432132342(34)323134(32)43413243424321234321432412343212(34)1341234' '24321(34)(32)3432134(32)3431(34)323432(34)(32)3432(34)3'};
the matched strings may be retrieved as:
a23 = get_matches('23', str_cell);
a23 = [a23{:}];
Looping over possible "prefixes" is trivial.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.