This repository has been archived by the owner on Jan 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistnorm.m
59 lines (53 loc) · 1.57 KB
/
histnorm.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function varargout = histnorm(varargin)
% HISTNORM Histogram normalized
% [...] = HISTNORM(...) works like HIST, but the frequency is normalized
% so that area sum is 1.
%
% Bonus usage!
% [...] = HISTNORM(..., 'plot') plots and returns the output arguments.
% Be sure 'plot' is the last argument.
%
% Example:
% data = randn (10000, 1);
% [xo,no] = histnorm(data, 101, 'plot');
% hold on
% plot (no, normpdf(no), 'r');
% hold off
%
% See also: HIST.
% Copyright 2009 DWTFYW.
% check whether plot is done
doPlot = 0;
if ischar (varargin{end}) && strcmpi ('plot', varargin{end})
doPlot = 1;
varargin = varargin(1:end-1);
elseif nargout == 0
doPlot = 1;
end
% normalize so the "area" is 1
[xo,no] = hist (varargin{:});
binwidths = diff ([min(varargin{1}) no(1:end-1)+diff(no)/2 max(varargin{1})]);
xonorm = xo/sum (xo .* binwidths);
varargout = {xonorm, no};
varargout = varargout(1:nargout);
% do plot
if doPlot
cax = axescheck(varargin{:});
% % bored way: bar plot
% if isempty (cax)
% bar (no, xonorm, 'hist');
% else
% bar (cax, no, xonorm, 'hist');
% end
% funny way: modify vertices of bar plot
hist (varargin{:});
if isempty (cax)
cax = gca;
end
ch = findobj (get (cax, 'children'), 'type', 'patch'); ch = ch(1);
vertices = get (ch, 'vertices');
for idx = 1:numel (xonorm)
vertices((idx-1)*5+[3 4],2) = xonorm(idx); % hope it works :)
end
set (ch, 'vertices', vertices);
end