Trouble displaying the final count

조회 수: 4 (최근 30일)
Zachary Dove
Zachary Dove 2019년 2월 13일
편집: Kevin Phung 2019년 2월 13일
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
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
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개)

카테고리

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