count the number a letter appears in a string and plot a histogram
조회 수: 11 (최근 30일)
이전 댓글 표시
I have to write a function that counts the number a base of DNA repeats itself inside of a string and then plot a histogram with the function 'histogram'. Here is my code to find the number of bases and it works fine
n=length(s);
if n>1000
disp('errore dimensione');
return
end
A=0;
C=0;
G=0;
T=0;
for i=1:n
switch s(i)
case 'A'
A=A+1;
case 'C'
C=C+1;
case 'G'
G=G+1;
case 'T'
T=T+1;
otherwise
disp('questo non è DNA');
return
end
end
vec=[A , C , G , T];
From here i want a histogram with x-axis representing the letters and y-axis representing the frequency each letter appears inside a string, how can i do that?
댓글 수: 0
답변 (2개)
DGM
2023년 3월 18일
편집: DGM
2023년 3월 18일
I'm sure there are text analysis tools for this, but here's one basic way.
categories = 'ACGT';
% generate a test string
N = 20; % length of test string
teststr = categories(randi([1 numel(categories)],1,N))
% find character frequencies
% if you just want character counts, don't divide
freq = sum(teststr == categories.',2)/numel(teststr)
% display as a bar chart
bar(freq)
xticklabels(num2cell(categories))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

