Trouble displaying the final count
조회 수: 4 (최근 30일)
이전 댓글 표시
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
TADA
2019년 2월 13일
편집: TADA
2019년 2월 13일
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'));
채택된 답변
Kevin Phung
2019년 2월 13일
편집: Kevin Phung
2019년 2월 13일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!