function to find longest string of consecutive ones

1 vue (au cours des 30 derniers jours)
SIOUDA abdel hakim
SIOUDA abdel hakim le 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

Réponse acceptée

MaryD
MaryD le 8 Jan 2020
Modifié(e) : MaryD le 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

Plus de réponses (1)

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

Catégories

En savoir plus sur Particle & Nuclear Physics dans Help Center et File Exchange

Tags

Produits


Version

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by