How to output the longest character string from two specified characters using nested for loop?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
% 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 Kommentare
Antworten (1)
Walter Roberson
am 20 Jan. 2023
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 Kommentare
Siehe auch
Kategorien
Mehr zu MATLAB Report Generator finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!