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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by