How do you generate a Cumulative Histogram on R2014a?
47 ビュー (過去 30 日間)
古いコメントを表示
I am trying to produce a Cummulative Histogram on MatLab so I can work out the 50% and 99% Percentiles in a set of data. I was shown and given a piece of code that will work on a newer version of MatLab but the only versions I have access to is MatLab R2014a which I have learned does not use the same commands as the newer versions. The section of code I am trying to use is shown below.
figure
histogram(wSpd,'Normalization','cdf'); % plot the cumulative histogram
y = quantile(wSpd, [0.5 0.99]); % extract the 50th and 99th quantiles (median and extreme)
As far as I know, hist is one of the options, but I have not been able to find any documentation for 2014a, only 2018a. Is there a way of doing what this section of code does in R2014a?
0 件のコメント
回答 (2 件)
Image Analyst
2018 年 7 月 5 日
You can get the counts with histcounts(), and then use cumsum(counts).
2 件のコメント
Image Analyst
2018 年 7 月 6 日
Did you look up those functions? You just do
[counts, bins] = histcounts(data);
cdf = cumsum(counts);
Luckily, Anton gave you a full demo below in his answer.
Anton Semechko
2018 年 7 月 5 日
編集済み: Anton Semechko
2018 年 7 月 5 日
Here is an example of how to use 'histcounts' and estimate percentiles:
% Simulate data; N samples from standard Guassian PDF
N=1E4;
X=randn(N,1);
% Bin data into B bins of equal size
X_min=min(X);
X_max=max(X);
B=50; % # of bins between X_min and X_max
dB=(X_max-X_min)/B; % bin size
BE=(X_min-dB):dB:(X_max+dB); % bin edges; first and last edges are at X_min-dB and X_max+dB
H=histcounts(X,BE); % histogram of X
MDF=H/N; % mass density function
Bc=(BE(2:end)+BE(1:(end-1)))/2; % bin centroids
% Cumulative mass function
CMF=cumsum(MDF);
% Esimate 50-th and 99-th percentiles
p=[50 99]/100;
Xp=zeros(size(p));
X_srt=sort(X);
CMF_raw=cumsum(ones(1,N))/N;
for i=1:numel(p)
[Ya,id_a]=find(CMF_raw<=p(i),1,'last');
[Yb,id_b]=find(CMF_raw>=p(i),1,'first');
if (Ya-p(i))<eps || id_a==id_b
Xp(i)=X_srt(id_a);
continue
end
[Xa,Xb]=deal(X_srt(id_a),X_srt(id_b));
m=(Yb-Ya)/(Xb-Xa);
if abs(m)<eps
Xp(i)=(Xa+Xb)/2;
else
Xp(i)=(p(i)-Ya)/m+Xa;
end
end
% Visualize MDF along with specified percentiles
figure('color','w')
ha=subplot(1,2,1);
h=bar(Bc,MDF);
set(h,'FaceColor',0.75*[1 1 1],'EdgeColor','k')
hold on
YLim=get(ha,'YLim');
col=zeros(numel(p),3);
for i=1:numel(p)
h=plot(Xp(i)*ones(2,1),YLim(:),'--','LineWidth',2);
col(i,:)=get(h,'Color');
end
set(get(ha,'Title'),'String','Normalized Histogram','FontSize',20)
% Visualize CDF
ha=subplot(1,2,2);
h=bar(Bc,CMF);
set(h,'FaceColor',0.75*[1 1 1],'EdgeColor','k')
hold on
XLim=get(ha,'XLim');
for i=1:numel(p)
plot(Xp(i)*ones(2,1),[0;p(i)],'--','LineWidth',2,'Color',col(i,:))
plot([XLim(1);Xp(i)],p(i)*ones(2,1),'--','LineWidth',2,'Color',col(i,:))
end
set(get(ha,'Title'),'String','Cumulative Mass Function','FontSize',20)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Histograms についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!