![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178675/image.png)
Creating Bins in a Histogram
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
jgillis16
le 12 Oct 2015
Réponse apportée : Star Strider
le 12 Oct 2015
I have a data set with the first column being the years and the second column being the distances. I need to create a histogram that bins how many of these distances occur during the same year, and then occurring every 10 years. (For example, 1880-1890 0, 1890-1900 1, 1900-1910 1).
I have attached the text file.
0 commentaires
Réponse acceptée
Star Strider
le 12 Oct 2015
You have one year recorded for each distance, so do the histogram on the years. This doesn’t account for the distances (I’m not exactly certain how to fit them into a histogram by year in the absence of plot3, if you want to somehow include them), but will plot the histogram of the discoveries by year and decade.
The code:
fidi = fopen('jgillis16 oo20copy.txt', 'rt');
Dc = textscan(fidi, '%f%f', 'CollectOutput',1, 'TreatAsEmpty','~');
fclose(fidi);
D = cell2mat(Dc);
years = D(:,1);
distances = D(:,2);
yrbin = [min(years):max(years)]-0.5;
dcbin = 10*[floor(min(years)/10) : 1 : floor(max(years)/10)]+5;
figure(1)
subplot(2,1,1)
hist(years, yrbin)
grid
subplot(2,1,2)
hist(years, dcbin)
grid
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178675/image.png)
0 commentaires
Plus de réponses (1)
Image Analyst
le 12 Oct 2015
The data you posted really doesn't have enough observations per decade to make a good histogram. There are just a handful per decade and usually only one or 2 per year. Nonetheless, you can use hist3() in the Stats toolbox to give counts per decade, and then per year in each decade.
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!