How do you generate a Cumulative Histogram on R2014a?

50 visualizzazioni (ultimi 30 giorni)
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?

Risposte (2)

Image Analyst
Image Analyst il 5 Lug 2018
You can get the counts with histcounts(), and then use cumsum(counts).
  2 Commenti
Sam Harper-Barber
Sam Harper-Barber il 5 Lug 2018
And how would that be put into the code? Say by changing the first line to hist, removing the normalisation and cdf and then having a new line with cumsum which I assume can do the same thing?
Image Analyst
Image Analyst il 6 Lug 2018
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.

Accedi per commentare.


Anton Semechko
Anton Semechko il 5 Lug 2018
Modificato: Anton Semechko il 5 Lug 2018
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)

Prodotti


Release

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by