필터 지우기
필터 지우기

Find prefix and postfix in a string

조회 수: 13 (최근 30일)
pamela sulis
pamela sulis 2015년 11월 18일
댓글: pamela sulis 2015년 11월 18일
Hi! I have a cell array (106x1) of strings, I want to find a prefix in every string and, after find the postfix, count the number of different items. Example
'0(012)(02)3(26)'
'(03)2(12)04)'
'(45)(02)(35)21'
'46(15)212'
For the prefix '01' the postfix are:
'(_2)(02)3(26)'
'(_2)04'
'3'
now i want to count the items more frequent: 0, 2, _2
Can you help me? Thanks
  댓글 수: 2
Guillaume
Guillaume 2015년 11월 18일
In your last two lines, I can't see any '01', so shouldn't the output be:
'(_2)(02)3(26)'
'(_2)04'
''
''
I also note that in your output you include the '(' that is before the prefix (and hence is not a postfix). Is that correct and is there any other symbol that follows this rule?
You've not explained what constitute an item, so I've no idea how you come about '0', '2', and '_2' being the most frequent. Why isn't '(' or ')' the most frequent item?
pamela sulis
pamela sulis 2015년 11월 18일
편집: pamela sulis 2015년 11월 18일
Yes, I made a mistake! After I have found the prefix, I want to find the elements more frequent in postfix. That in this case are 0,2 and (_2) but i have no idea how to do this. I have tried to find postfix in this way:
for k=1:106
a(k)= regexp(TrajCompact(k,1), '01', 'split');
end

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2015년 11월 18일
You can't have a regex that returns the before and after of a sequence of characters without that sequence of characters, but you can do your search and replace in two steps
in = {'0(012)(02)3(26)'
'(03)2(012)04)'
'(45)(02)(35)21'
'46(15)212'};
matches = regexp(in, '\(?01.*', 'match', 'once');
out = regexprep(matches, '01', '_') %note that you could simply use strrep
As for your question about frequency, I still have no idea what constitute an item or element.
  댓글 수: 3
Guillaume
Guillaume 2015년 11월 18일
Well , 0, 1, 2, 3, 4,... are individual digits. Yet, you also include _2 in your items which is made of two characters.
pamela sulis
pamela sulis 2015년 11월 18일
thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by