How to make histogram values be sum of the values of each bin?

11 vues (au cours des 30 derniers jours)
Amit Ifrach
Amit Ifrach le 24 Août 2021
Modifié(e) : Pavan Sahith le 12 Oct 2023
לק"י
Hi guys,
I have 2 vectors of area values that I want to put on a histogram. I want to make it a semilog (logarithmic Y axis), but want the histogram values to reffer the total area of incidents of each bin, instead of the number of incidents in it.
Histogram regular command of y=histogram(x, etc..) gives only the plot, and I couldn't see in the histogram info page the form of presentation I look for.
Is there any short way to do so? if not, which command and stratagies would you suggest to get the goal?
the code I used so far to plot the hists:
edges=[0 0:10000:500000 600000];
figure(1)
aCD3tothist=histogram(aCD3tot, edges, 'Normalization','probability', 'FaceColor', 'r');
set(gca,'YScale','log')
title('aCD3 tot')
xlabel('area (nm^2)')
ylabel('perc.')
hold on
aCD45tothist=histogram(aCD45tot, edges, 'Normalization','probability', 'FaceColor', 'b');
hold off
I tried to bin the original data, and managed to do so:
figure(10)
[aCD3tothstcnts,edges] = histcounts(aCD3tot,edges)
bar(aCD3tothstcnts, 'FaceColor', 'r')
hold on
set(gca,'YScale','log')
title('aCD45 tot')
xlabel('area (nm^2)')
ylabel('perc.')
[aCD45tothstcnts,edges] = histcounts(aCD45tot,edges)
bar(aCD45tothstcnts, 'FaceColor', 'b')
hold off
But again, the histcounts command gives the number of incidents that fall to each bin.
I can't seem to have a way to know the avarage area of each bin (and then jsut simply muliply it in the number of incidents) or the area of each incident in each bin to sum it up somehow.
I hope it was clear enough. thank you very much!

Réponses (1)

Pavan Sahith
Pavan Sahith le 12 Oct 2023
Modifié(e) : Pavan Sahith le 12 Oct 2023
Hi Amit,
I understand that you want to calculate the sum of values (elements of your area vectors) that fall under each bin.
For that you can use combination of discretize’ and accumarray.You can refer to the example code below.
data=1:5;
edges=0:2:6;
% as per above data
% 1 fall under edge 1 (0 to 2)
% 2,3 fall under edge 2 (2 to 4)
% 4,5 fall under edge 3 (4 to 6)
binIndices= discretize(data,edges)
binIndices = 1×5
1 2 2 3 3
sum=accumarray(binIndices',data')
sum = 3×1
1 5 9
Please refer to the MathWorks documentation links to know more about
You can refer to the following code :
[counts]=histcounts(data,edges);
% Calculate bin widths
binWidths = diff(edges)
binWidths = 1×3
2 2 2
% Calculate bin areas
binAreas = binWidths .* counts
binAreas = 1×3
2 4 4
Hope it helps.

Catégories

En savoir plus sur Data Distribution Plots 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!

Translated by