count the number a letter appears in a string and plot a histogram
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Stephen23
le 18 Mar 2023
V = categorical(["A","C","G","T"]);
W = V(randi(4,1,23))
histogram(W)
0 commentaires
DGM
le 18 Mar 2023
Modifié(e) : DGM
le 18 Mar 2023
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 commentaires
Voir également
Catégories
En savoir plus sur Histograms dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!