Plot a histogram from given data.
Afficher commentaires plus anciens
I would like to plot a frequencies histogram from my data, but turn up it din't shows up like what I expected. Could somebody help me? I would like the result same as picture below

clear;
clc;
B = readtable('hw3.txt','ReadVariableName',false,'delimiter',':');
data2=B(:,2);
data3=table2array(B(:,2));
data4=str2double(split(data3,','));
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
g = 0;
h = 0;
j = 0;
k = 0;
for i = 1:45
if data4(i) >=0 && data4(i) <= 9
a = a + 1;
elseif data4(i) >= 10 && data4(i) <= 19
b = b + 1;
elseif data4(i) >= 20 && data4(i) <= 29
c = c + 1;
elseif data4(i) >= 30 && data4(i) <= 39
d = d + 1;
elseif data4(i) >= 40 && data4(i) <= 49
e = e + 1;
elseif data4(i) >= 50 && data4(i) <= 59
f = f + 1;
elseif data4(i) >= 60 && data4(i) <= 69
g = g + 1;
elseif data4(i) >= 70 && data4(i) <= 79
h = h + 1;
elseif data4(i) >= 80 && data4(i) <= 89
j = j + 1;
elseif data4(i) >= 90 && data4(i) <= 100
k = k + 1;
end
end
fprintf('0~9: %d\n', a);
fprintf('10~19: %d\n', b);
fprintf('20~29: %d\n', c);
fprintf('30~39: %d\n', d);
fprintf('40~49: %d\n', e);
fprintf('50~59: %d\n', f);
fprintf('60~69: %d\n', g);
fprintf('70~79: %d\n', h);
fprintf('80~89: %d\n', j);
fprintf('90~100: %d\n', k);
x = 0 : 10 : 100 ;
y = 7;
histogram(data4);
xlabel('Group Interval');
ylabel('No. of students');
title('Histogram of Grades');
Réponses (2)
Arif Hoq
le 15 Mar 2022
try this:
B = readtable('hw3.txt','ReadVariableName',false,'delimiter',':');
data2=B(:,2);
data3=table2array(B(:,2));
data4=str2double(split(data3,','));
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
g = 0;
h = 0;
j = 0;
k = 0;
for i = 1:45
if data4(i) >=0 && data4(i) <= 9
a = a + 1;
elseif data4(i) >= 10 && data4(i) <= 19
b = b + 1;
elseif data4(i) >= 20 && data4(i) <= 29
c = c + 1;
elseif data4(i) >= 30 && data4(i) <= 39
d = d + 1;
elseif data4(i) >= 40 && data4(i) <= 49
e = e + 1;
elseif data4(i) >= 50 && data4(i) <= 59
f = f + 1;
elseif data4(i) >= 60 && data4(i) <= 69
g = g + 1;
elseif data4(i) >= 70 && data4(i) <= 79
h = h + 1;
elseif data4(i) >= 80 && data4(i) <= 89
j = j + 1;
elseif data4(i) >= 90 && data4(i) <= 100
k = k + 1;
end
end
fprintf('0~9: %d\n', a);
fprintf('10~19: %d\n', b);
fprintf('20~29: %d\n', c);
fprintf('30~39: %d\n', d);
fprintf('40~49: %d\n', e);
fprintf('50~59: %d\n', f);
fprintf('60~69: %d\n', g);
fprintf('70~79: %d\n', h);
fprintf('80~89: %d\n', j);
fprintf('90~100: %d\n', k);
x = 0 : 10 : 100 ;
y = 7;
output=[a b c d e f g h j k]'
% h = histogram(output,'BinWidth',0.3)
bar(output,'BarWidth',0.05)
xlabel('Group Interval');
ylabel('No. of students');
title('Histogram of Grades');
1 commentaire
Allen Lee
le 15 Mar 2022
Geoff Hayes
le 15 Mar 2022
@Allen Lee - perhaps you can simplify your code to use the histogram edges so that you don't have to sort the data yourself. Something like
edges = [0 10 20 30 40 50 60 70 80 90 100];
histogram(data, edges);
1 commentaire
Allen Lee
le 15 Mar 2022
Catégories
En savoir plus sur Descriptive Statistics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!