Loop not working to find min,max,mean

2 Ansichten (letzte 30 Tage)
Marios Christofides
Marios Christofides am 12 Jul. 2020
Kommentiert: Geoff Hayes am 13 Jul. 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 Kommentar
Stephen23
Stephen23 am 13 Jul. 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')

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by