Loop not working to find min,max,mean

4 visualizzazioni (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 13 Lug 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')

Accedi per commentare.

Risposta accettata

Geoff Hayes
Geoff Hayes il 13 Lug 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 Commenti
Marios Christofides
Marios Christofides il 13 Lug 2020
Thank you. How should I change nump to be a vector while still being updated?
Geoff Hayes
Geoff Hayes il 13 Lug 2020
Marios - what are you trying to save on each iteration of the loop?

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by