function to find longest string of consecutive ones
조회 수: 1 (최근 30일)
이전 댓글 표시
hi all, i'm new here. i'v got some difficulties to draft a function to find longest string of consecutive ones (DNA sequence)
for ex: AAAGGTCCATTTTTTTAA
it shows TTTTTTT
댓글 수: 0
채택된 답변
MaryD
2020년 1월 8일
편집: MaryD
2020년 1월 8일
This is quick made example how it may work. Nbest tells number of letters in the longest string and Lbest tells what letters the string consists of
clear;clc;close all;
str='AAATTTTTCCCCCAATTCCC';
AGTCstr='AGTC';
Strsize=size(str);
AGTCsize=size(AGTCstr);
temp=0;
k=0;
Nbest=0;
for i=1:Strsize(2)
for j=1:AGTCsize(2)
h=strcmp(str(i),AGTCstr(j));
if h==0
continue
end
if h==1
k=k+1;
if strcmp(temp,str(i))==0
if k>=Nbest
Nbest=k;
Lbest=temp;
end
k=0;
end
temp=str(i);
end
end
end
댓글 수: 0
추가 답변 (1개)
Image Analyst
2020년 1월 8일
Use findgroups() and regionprops():
s = 'AAAGGTCCATTTTTTTAA' - 'A'
g = findgroups(s)
for k = 1 : max(g)
props = regionprops(g == k, 'Area');
largestAreas(k) = max([props.Area]);
fprintf('The largest stretch of group %d is %d.\n', k, largestAreas(k));
end
You'll see in the command window:
The largest stretch of group 1 is 3.
The largest stretch of group 2 is 2.
The largest stretch of group 3 is 2.
The largest stretch of group 4 is 7.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!