function to find longest string of consecutive ones

1 view (last 30 days)
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

Accepted Answer

MaryD
MaryD on 8 Jan 2020
Edited: MaryD on 8 Jan 2020
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

More Answers (1)

Image Analyst
Image Analyst on 8 Jan 2020
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.

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Tags

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!