How to accumulate histograms?

2 visualizzazioni (ultimi 30 giorni)
aneps
aneps il 25 Nov 2014
Risposto: Guillaume il 25 Nov 2014
I have data ranging from 0 to 1e5 (in microseconds). I want to make histogram for each successive 2.68 microseconds binning for 0.05 microseconds and finally accumulate all the histograms. I have following attempt:
a=load('Data.dat')
Range=2.68; % For every successive 2.68micro seconds, I want to plot histogram
n= max(a)/Range; % This tells how many such ranges will be in the data
low=min(a);
high=min(a)+Range;
x=low:0.05:high;
y=hist(a,x);% This is the first histogram for the first 2.68 microsecond
figure
semilogy(x,y,'-ob')
for num=1:1:n-1 % This loop is to make histogram for successive 2.68 microseconds
low=low+Range;
high=high+Range;
x=low:0.05:high;
y=y+hist(a,x-low); % This (I AM NOT SURE) is intended to accumulate the histogram for each loop.
end
figure
semilogy(x-low,y,'-or'); % FInal accumulated histogram.
This is the program I made. But it seems the histogram is not getting accumulated fore each run of the loop. Can anyone help me to accumulate the histogram? Or any other better ways to accumulate histogram for the successive ranges?

Risposte (1)

Guillaume
Guillaume il 25 Nov 2014
Your code is nearly right.
The problem is that matlab has several histogram functions (four as of R2014b) and you used the wrong one for what you want to do.
hist uses the x vector as bin centres with the first bin extending to cover all values before the 1st centre and the last bin extending to cover all values above the last centre. That means any of your histogram will include all the values in a regardless of your range.
As it looks like you intended x as a vector of edges anyway, you should use histc (or histcounts as of r2014b) instead of hist.
There comes the issue of what you want to do with the last bin. Your last bin is from 2.60 to 2.65. As you wrote your code, you never include the values from 2.65 to 2.68. Maybe you want x as
x = [low:0.05:high high+mod(Range, 0.05)];
Note that histc and histcounts behave differently with regards to the last bin.
Finally, in your loop, you pass x-low as bins. So effectively, you're always passing the initial bins. The -low shouldn't be there.

Community Treasure Hunt

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

Start Hunting!

Translated by