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));

 採用された回答

Ridwan Alam
Ridwan Alam 2019 年 12 月 13 日
編集済み: Ridwan Alam 2019 年 12 月 13 日

1 投票

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 件のコメント

Tania Islam
Tania Islam 2019 年 12 月 13 日
編集済み: Tania Islam 2019 年 12 月 13 日
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
Adam Danz
Adam Danz 2019 年 12 月 13 日
+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
Adam Danz
Adam Danz 2019 年 12 月 13 日
Set the BinWidth property in histogram() or in histcounts()
Example
histcounts(x,'BinWidth',10)
Ridwan Alam
Ridwan Alam 2019 年 12 月 13 日
編集済み: Ridwan Alam 2019 年 12 月 13 日
Thanks, Adam. I updated the answer with 'BinWidth'.
Tania, please let us know if there is any confusion.
Tania Islam
Tania Islam 2019 年 12 月 14 日
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.
Ridwan Alam
Ridwan Alam 2019 年 12 月 14 日
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 2019 年 12 月 14 日
編集済み: Adam Danz 2019 年 12 月 15 日
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 2019 年 12 月 14 日
編集済み: Ridwan Alam 2019 年 12 月 15 日
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.
Tania Islam
Tania Islam 2019 年 12 月 16 日
Thank you so much for your kind suggestions and time.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

タグ

質問済み:

2019 年 12 月 13 日

コメント済み:

2019 年 12 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by