Trouble displaying the final count
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zachary Dove
am 13 Feb. 2019
Bearbeitet: Kevin Phung
am 13 Feb. 2019
Hi all, I am having trouble with a portion of my code. I have created this code to search through a character array of DNA and pick out how many time the sequence CAT appears. The code works fine and displays the correct answer, however i need it not to show the count from 1-126 but just the final count at 126. Thanks for the help! Code is located below
load HW2_part2_data.mat
count = 0;
for i = 1: length(DNA)-1
if DNA(i,1)=='C'
if DNA(i+1,1)=='A'
if DNA(i+2,1)=='T'
count = count + 1;
end
end
end
end
1 Kommentar
TADA
am 13 Feb. 2019
Bearbeitet: TADA
am 13 Feb. 2019
Not sure I understand what you mean
but you have a bug in your loop index, you need to stop at length(DNA)-2.
Moreover, you can replace the loop with a simpler strfind if your DNA vector would be a row vector instead:
% generate random DNA sequence
bases = 'ATGC';
DNA = bases(randi(4, 400, 1));
% cound number of CAT sequences in the DNA chain
count = numel(strfind(DNA, 'CAT'));
Akzeptierte Antwort
Kevin Phung
am 13 Feb. 2019
Bearbeitet: Kevin Phung
am 13 Feb. 2019
just do this:
search = 'CAT';
locate = regexp(DNA,search)
count = numel(locate{1}) %number of times CAT appears
edit: it seems like by the time I made the edit above, you accepted my previous answer which i thought was wrong. So i'll just repost it just in case both come in handy:
Prev soln:
sum(contains(DNA,'CAT'))
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!