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

采纳的回答

MaryD
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

更多回答(1 个)

Image Analyst
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.

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

标签

产品


版本

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by