function to find longest string of consecutive ones

1 Ansicht (letzte 30 Tage)
SIOUDA abdel hakim
SIOUDA abdel hakim am 8 Jan. 2020
Beantwortet: Image Analyst am 8 Jan. 2020
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

Akzeptierte Antwort

MaryD
MaryD am 8 Jan. 2020
Bearbeitet: MaryD am 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

Weitere Antworten (1)

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

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by