Loop not working to find min,max,mean

조회 수: 4 (최근 30일)
Marios Christofides
Marios Christofides 2020년 7월 12일
댓글: Geoff Hayes 2020년 7월 13일
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
Stephen23
Stephen23 2020년 7월 13일
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')

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by