How to Count occurrences?

2 views (last 30 days)
Cre'Shawn Dabney
Cre'Shawn Dabney on 8 Oct 2019
Answered: Jan on 10 May 2021
Given a text: ‘tagtacagccagtagagttgattccaaggaagtccggctgttgtagagtagc’ and a three-character target pattern (for example: ‘TAG’ ), your program should identify all target patterns in the text and output the number of occurrences of the target pattern.
Sample output Text:tagtacagccagtagagttgattccaaggaagtccggctgttgtagagtagctag
Target:TAG
Number of TAG:5
Target:tag
Number of tag:5
Target:GTa
Number of cag:2
Target:exit Program terminated!
  3 Comments
Priyadharshini A.R
Priyadharshini A.R on 10 May 2021
Hello Dabney,
The loop must go on till n-m+1 so that it can traverse till last element in text

Sign in to comment.

Answers (2)

Jan
Jan on 10 May 2021
t = 'tagtacagccagtagagttgattccaaggaagtccggctgttgtagagtagc';
tag = 'tag';
result = sum(strfind(lower(t), lower(tag)))

Alaster Meehan
Alaster Meehan on 8 Oct 2019
strcmpi ignors case
Cheers Alaster
  1 Comment
Cre'Shawn Dabney
Cre'Shawn Dabney on 8 Oct 2019
Thank you for your answer. I am still confused on how to write this code. Could you possibly demonstrate how? If not thank you so much for your participation!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!