How to find prefix and postfix in a recursive way

1 view (last 30 days)
Hi! I have a array of strings: I want to find prefix and postfix; after find postfix I want to find prefix and postfix of it and so on.
Example
strings prefix postfix ---> prefix postfix ---> prefix postfix ---> prefix postfix
23414 2 3414 3 414 4 14 1 4
342(1) 3 42(1) 4 2(1) 2 (1) 1
34 3 4 4
231 2 31 3 1 1
I have tried to do it with this code:
semanticTrajCompact(1,4).TrajCompact=(semanticTrajCompact(1,4).TrajCompact(~cellfun('isempty',semanticTrajCompact(1,4).TrajCompact)));
semanticTrajCompact(1,4).TrajCompact=sort(semanticTrajCompact(1,4).TrajCompact);
for k=1:size(semanticTrajCompact(1,4).TrajCompact,1)
if ~isempty(semanticTrajCompact(1,4).TrajCompact{k,1})
if semanticTrajCompact(1,4).TrajCompact{k,1}(1)=='('
PrefixPostfix(1,4).prefix{k,1}=semanticTrajCompact(1,4).TrajCompact{k,1}(2:3);
PrefixPostfix(1,4).postfix{k,1}=semanticTrajCompact(1,4).TrajCompact{k,1}(5:end);
else
PrefixPostfix(1,4).prefix{k,1}=semanticTrajCompact(1,4).TrajCompact{k,1}(1);
PrefixPostfix(1,4).postfix{k,1}=semanticTrajCompact(1,4).TrajCompact{k,1}(2:end);
end
end
end
prefixTree(1,4).prefixTree(:,1)=semanticTrajCompact(1,4).TrajCompact;
prefixTree(1,4).prefixTree(:,2)=PrefixPostfix(1,4).prefix;
prefixTree(1,4).prefixTree(:,3)=PrefixPostfix(1,4).postfix;
After I have a problems to find prefix and postfix to leave from the result of
prefixTree(1,4).prefixTree(:,3)=PrefixPostfix(1,4).postfix;
Can you help me?

Accepted Answer

Stephen23
Stephen23 on 12 Jan 2016
Edited: Stephen23 on 12 Jan 2016
C = {'23414','342(1)','34','231'};
pfx = cellfun(@(s)regexp(s,'\d','match'),C,'UniformOutput',false);
fun = @(s)arrayfun(@(n)s(n:end),regexp(s,'(\d|\(\d\))'),'UniformOutput',false);
sfx = cellfun(fun,C,'UniformOutput',false);
and the two output cell arrays contain:
>> pfx{:}
ans =
'2' '3' '4' '1' '4'
ans =
'3' '4' '2' '1'
ans =
'3' '4'
ans =
'2' '3' '1'
>> sfx{:}
ans =
'23414' '3414' '414' '14' '4'
ans =
'342(1)' '42(1)' '2(1)' '(1)'
ans =
'34' '4'
ans =
'231' '31' '1'

More Answers (0)

Categories

Find more on External Language Interfaces 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!