Question on summing bins in hist

2 views (last 30 days)
Hi! I'm still learning how to use matlab and I'm trying to bin together my data into size ranges (0.25-0.5, 0.5-1, 1-2, 2-4,.....1024-2048), then taking the number of values that fall into each bin, adding them up then dividing them by the width of the bin. For ease and clarity, I'm only using one dataset to show here as an example.
edges = 2.^(-2:10); %This defines our bins with octave spacing
b=Initial.esd; %This is a 1504x1 double, containing values in such as 20.3847, 1024.5, etc
b1 = hist(b,edges); %This line groups each value in b into my 13 bins (0.25-0.5, etc); produces a 1 x 13 double
b2 = (sum(b1./edges)); %This line sums the values in each bin and divides them by edges to give us 13 values for each 13 bin; should produce a 1 x 13 double
However the above code sums all the bins together, not sum each bin. I found another code whilst googling and tried it but got an error of "Error using accumarray. First input SUBS must contain positive integer subscripts."
edges = 2.^(-2:10); %This defines our bins with octave spacing
b=Initial.esd; %This is a 1504x1 double, containing values in such as 20.3847, 1024.5, etc
[~, idx] = histc(b,edges); %this is grouping each value into a numbered bin
binsumsB = (accumarray(idx,b)); %this line sums the values of each bin
Any help would be appreciative, I've been struggling with this for a couple days!

Accepted Answer

Steven Lord
Steven Lord on 2 Jun 2021
Instead of using hist, I recommend using histogram. You will want to set the 'Normalization' option when you call histogram.
  3 Comments
Steven Lord
Steven Lord on 3 Jun 2021
Do you want the graphics object (in which case you should call histogram and if you need to operate on the data access the Values property of the histogram object) or do you just need the bin counts and edges (in which case call histcounts.)
Based on the description I believe you want to call either histogram or histcounts using the 'countdensity' value for the 'Normalization' name-value pair. See the table for that argument in the documentation for more information.
Natalie Klueppelberg
Natalie Klueppelberg on 3 Jun 2021
Ok I wil browse around the histogram documentation, thank you for the help and taking time to comment!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!