-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistogram.m
45 lines (38 loc) · 1.23 KB
/
Histogram.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
%MIT License
%Copyright (c) 2019 Sherman Lo
%CLASS: HISTOGRAM
%A custom histogram class
%
%Plots the histogram (frequency density) as a line function
%How to use:
%pass the data, and optionally the bin edges, via the constructor
%class the method plot
%properties of the histogram can be extracted from the member variables
classdef Histogram < handle
properties (SetAccess = protected)
n; %count for each bin
edges; %the border of each bin
binWidth; %the width of each bin
freqDensity; %freq density of each bin, count / binwidth
end
methods (Access = public)
%CONSTRUCTOR
%PARAMETERS:
%X: vector of data
%edges (optional): the edges of the bins, vector size numel(X)+1, if empty, uses bins in
%the default MATLAB function histcounts
function this = Histogram(X, edges)
if nargin == 1
[this.n, this.edges] = histcounts(X);
elseif nargin == 2
[this.n, this.edges] = histcounts(X, edges);
end
this.binWidth = this.edges(2:end) - this.edges(1:(end-1));
this.freqDensity = [0, this.n./this.binWidth, 0];
end
%METHOD: PLOT
function plot(this)
stairs([this.edges(1),this.edges], this.freqDensity);
end
end
end