Loop not working to find min,max,mean

4 visualizaciones (últimos 30 días)
Marios Christofides
Marios Christofides el 12 de Jul. de 2020
Comentada: Geoff Hayes el 13 de Jul. de 2020
I am running a for loop to determine the min,max, and mean of a DNA sequence. When I try to run the program it outputs zero as the value of the min max and mean. I can't find a solution to why the loop is not running properly. Does anyone have a fix?
load('chr1_sect.mat')
numBases = length(dna);
startPoint = 0; nump = 0;
for k = 1:3:numBases-2
if startPoint == 0
if dna(k) == '1' && dna(k+1) == '4' && dna(k+2) == '3'
startpoint = k;
end
else
if ((dna(k)) == '4' && dna(k+1) == '1' && dna(k+2)) == '1' ||...
(((dna(k))) == '4' && dna(k+1) == '1' && dna(k+2)) == '3' ||...
(((dna(k))) == '4' && dna(k+1) == '3' && dna(k+2)) == '1'
nump = nump +1;
SavedPoints(np,1) = startPoint;
SavedPoints(np,2) = k;
startPoint = 0;
end
end
end
x = min(nump)
y = max(nump)
z = mean(nump)
  1 comentario
Stephen23
Stephen23 el 13 de Jul. de 2020
Note that you can probably replace those repeated logical operations e.g.:
dna(k) == '1' && dna(k+1) == '4' && dna(k+2) == '3'
with simpler
strcmp(dna(k:k+2),'143')
or
isequal(dna(k:k+2),'143')

Iniciar sesión para comentar.

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 13 de Jul. de 2020
Marios - from your code
nump = nump +1;
will always be a scalar (1x1) value which seems incorrect. Should this be an array of values? As for why, nump is always zero
if startPoint == 0
if dna(k) == '1' && dna(k+1) == '4' && dna(k+2) == '3'
startpoint = k;
end
else
note how the condition is for startPoint == 0 but you then initialize startpoint = k....which is a different variable because of the lower-case p. Try changing this to
if startPoint == 0
if dna(k) == '1' && dna(k+1) == '4' && dna(k+2) == '3'
startPoint = k; % <---- note the difference
end
else
and see what happens.
  2 comentarios
Marios Christofides
Marios Christofides el 13 de Jul. de 2020
Thank you. How should I change nump to be a vector while still being updated?
Geoff Hayes
Geoff Hayes el 13 de Jul. de 2020
Marios - what are you trying to save on each iteration of the loop?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by