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!

 채택된 답변

Steven Lord
Steven Lord 2021년 6월 2일

0 개 추천

Instead of using hist, I recommend using histogram. You will want to set the 'Normalization' option when you call histogram.

댓글 수: 3

Natalie Klueppelberg
Natalie Klueppelberg 2021년 6월 3일
Hi! I've tried using histogram, only issue is that histogram does not allow me to divide my bins, ex: b2 = (sum(b1./edges));
Steven Lord
Steven Lord 2021년 6월 3일
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 2021년 6월 3일
Ok I wil browse around the histogram documentation, thank you for the help and taking time to comment!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by