Trouble displaying the final count
1 view (last 30 days)
Show older comments
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 Comment
TADA
on 13 Feb 2019
Edited: TADA
on 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'));
Accepted Answer
Kevin Phung
on 13 Feb 2019
Edited: Kevin Phung
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Genomics and Next Generation Sequencing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!