How can I get data from histogram ?

Hi,
I want a CDF curve from my dataset. For that, at first I am plotting the dataset into histogram using histogram function. My dataset contain error of times of 1000 samples. Among 1000, we may have only distinct 200 values (others are repetition, so I want the numbe of occurrance of each values). Thats why I plot them to histogram to get the values and the propbabaility of the values.
Priviously I was using %[counts_5_10, centers_5_10] = hist(data);. This code was fine for me. How can I get this information from the new histogram function?
In the previous code I cannot fixed the binwidth.
Following is my old and new code.
I cannot extract the X-axes dataset (unique values among the 1000)
old code:
%[counts_5_10, centers_5_10] = hist(data);
probability=counts_L_7(1,:)/1000;
CDF=cumsum(probability/sum(probability));
New code:
h = histogram(data,'BinWidth',0.005);
N= h.Values% repetition of number of error values
Edges = h.BinEdges% this is the error values
probability=N/1000;
CDF=cumsum(probability./sum(probability));

 Risposta accettata

Ridwan Alam
Ridwan Alam il 13 Dic 2019
Modificato: Ridwan Alam il 13 Dic 2019
I believe you are looking for histcounts() instead of histogram().
[counts, centers] = histcounts(data,'BinWidth',0.005);
probability=counts/1000; % =counts/sum(counts);
CDF=cumsum(probability/sum(probability));
Btw, if you don't need probability, the CDF can be directly calculated as follows:
[CDF, centers] = histcounts(data,'BinWidth',0.005,'normalization','cdf');

9 Commenti

Tania Islam
Tania Islam il 13 Dic 2019
Modificato: Tania Islam il 13 Dic 2019
Thank you for your answer.
Actually, I can dot that but I want to set my BinWidth also.
My data set is real number, for example, 0.001,0.005 sec
+1
There's nothing wrong with getting the values from the histogram handle but if you don't want to plot the histrogram, histcounts() is the way to go.
h = histogram(randn(1,100));
h.BinCounts % or h.Values
h.BinEdges
Set the BinWidth property in histogram() or in histcounts()
Example
histcounts(x,'BinWidth',10)
Ridwan Alam
Ridwan Alam il 13 Dic 2019
Modificato: Ridwan Alam il 13 Dic 2019
Thanks, Adam. I updated the answer with 'BinWidth'.
Tania, please let us know if there is any confusion.
Hi,
I am getting this error,
"Vectors must be the same length."
because I want to plot to the CDF on Y axes and Centers on the X-axes. But centers is 1 index larger than cdf.
Yes, because what you named “centers” are really the “edges” of the bins. So 10 bins have 11 edges. Please read the documentation for more details. To plot the CDF, you are free to choose any vector extracted from the “centers”. One example:
plot(centers(1:end-1),CDF)
Adam Danz
Adam Danz il 14 Dic 2019
Modificato: Adam Danz il 15 Dic 2019
Just to add to Ridwan's answer, the 2nd output to histcounts are actually the bin edges,
'centers' is a misleading name for that variable. The number of edges will always be +1 larger than the number of bins. To get the true centers of the bins,
centers = edges(2:end) - (edges(2)-edges(1))/2;
or
centers = edges(2:end) - BinWidth/2;
Ridwan Alam
Ridwan Alam il 14 Dic 2019
Modificato: Ridwan Alam il 15 Dic 2019
Adding Adam's suggestion to the answer:
BinWidth = 0.005;
[CDF, edges] = histcounts(data,'BinWidth',BinWidth,'normalization','cdf'); % feel free to use the other solution using 'counts'
centers = edges(2:end) - BinWidth/2;
% with this 'centers' will have the same size as CDF.
Thank you so much for your kind suggestions and time.

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by