How to output the longest character string from two specified characters using nested for loop?
조회 수: 2 (최근 30일)
이전 댓글 표시
% ptn is the string we are working with to find M and *
ptn = 'TFASDTTVFTSNLKQTPWCI*LLRRSLPLLPCGAR*TWMKLVVRPWAGWYQGYKTGLRRPIETGHVETEKTLGFLIGTDSLCLLVYFPTLRLLVVYPWTQRFFESFGDLSTPDAVMGNPKVKAHGKKVLGAFSDGLAHLDNLKGTFATLSELHCDKLHVDPENFRVSLWDA*CFLSPSFLWLSSCHRKGISNRVQFRMGNRRMIASVWKSQDRFSFFYLLFITIVFFCLILAFFFFLLRNFYYYT*CLNIVYNKRKYL*DTLSNLKKNFTQSA*YITIWNICVLICIFIISLLYFLLFLIDT*SLYIFMG*SVMF*YVYTY*PNQGNFAFVILKNAFFF*YTFLFILFLILSLISFFQGNNDTMYHASLHHSKE*Q**FLG*GNSNISAYKYFCI*IVTDVRGFILLIAATIQLPFCFYFMVGIRLDYSESKLGPFANHVHTSYLPPTAPGQRAGLCAGPSLWQRIHPTSAGCLSESGGWCG*CPGPQVSLSSLSCCPISIKGSFVP*VQLLNWGIL*RALSIWILPNKKHLFSL'
%starts and stops represents the two specified characters in the string called ptn
starts = strfind(ptn,'M');
stops = strfind(ptn,'*');
lengths = [];
genes={};
%foreach entry in starts: let "start" be that entry.
% i.e start=39.
%foreach entry in stops. let "stop" be that entry.
% i.e stop=21, or stop=36, or stop=172...
%find the length of this ORF: stop-start
%append to lengths.
%Identify this ORF text:
% ptn(start:stop)
for start = 1:numel(starts)
for stop = 1:numel(stops)
if stops(stop) > starts(start)
length = min(stops(stop))-(starts(start));
g = ptn(starts(start):stops(stop));
lengths = [lengths,length]
[longest_str,pos] = max(lengths)
end
end
end
genes = [genes,{g}];
% desired output: longest number of characters from 'M' to '*' in ptn
댓글 수: 0
답변 (1개)
Walter Roberson
2023년 1월 20일
You do not need nested loops. You can loop character by character. You can be in state "outside of string" or "inside". If you are in state outside and this character is not 'M' then keep going to the next character. If you are in state outside and this character is 'M' then set the counter to 1 and set the state to inside. If you are in state inside and the current character is '*' then take the maximu of (longest you found so far) and (current counter + 1) and that maximum is the longest string you found so far, and set the state to "outside".
When you reach the end of the string then regardless of whether you are inside or outside, report on the longest you found -- if you were in state inside then you are in the middle of matching an M-string but you did not find the * and so this current M-string is irrelevant.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!