필터 지우기
필터 지우기

How to find prefix and postfix in a recursive way

조회 수: 1 (최근 30일)
cat cat
cat cat 2016년 1월 12일
편집: Stephen23 2016년 1월 12일
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?

채택된 답변

Stephen23
Stephen23 2016년 1월 12일
편집: Stephen23 2016년 1월 12일
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'

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by