Question on summing bins in hist
Show older comments
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
More Answers (0)
Categories
Find more on Histograms 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!