Lets say we have following vector:
data=a=[2.3 3.2 4.1 6.2 7.3 6.4 5.5 4.3 3.2 2.6 1.7 3.4 4.5 5.7 6.8];
If we count the numbers only by using the hist-function, we get something like that:
[n xout]=hist(a,[1:1:max(a)])
n =
0 0 2 4 3 1 3 2
xout =
0 1 2 3 4 5 6 7
What i now want to make is to sum each element of a bin:
xout =
0 1 2 3 4 5 6 7
n =
0 0 2 4 3 1 3 2
binsum =
0 0 4 12.4 etc.
for the third bin for example i have n(3)=2 values that are between 1,5 and 2,5 (size of a bin):1.7 and 2.3 -> 1.7+2.3=4 -> binsum(3)=4
for the fourth bin i have n(4)=4 values that are between 2,5 and 3,5:3.2+3.2+3.4+2.6=12.4-> binsum(4)=12.4 etc.
is there a simple function which do this job?
If your histogram bins are defined by the array
minBin:binWidth:maxBin, then you can find the indices of the bins, and then sum up the data like this:Note that I don’t get two zeros first, because I use the bins you used as input to
hist, not the ones you apparently used to generate the outputs. Also, you can get the counts by replacingdatabyones(length(data),1).