Previous: , Up: General-Purpose Functions   [Contents][Index]


1.1.2 Hist

Hist is a Octave class which represents a multi-dimensional histogram. It is useful for accumulating data of a statistical nature and determining its properties.

A one-dimensional histogram is created with the following command:

octave> hgrm = Hist(1, {"lin", "dbin", 0.01})
hgrm = {histogram: count=0, range=[NaN,NaN]}

This histogram will store data in bins with linear boundaries of width 0.01. The Hist class can also store data in bins with logarithmic boundaries:

octave> Hist(1, {"log", "minrange", 0.02, "binsper10", 20})
ans = {histogram: count=0, range=[NaN,NaN]}

To add data to a histogram, use the addDataToHist() function. Let’s add some Gaussian-distributed data:

octave> hgrm = addDataToHist(hgrm, normrnd(10.7, 3.42, [1e4, 1]))
hgrm = {histogram: count=10000, range=[-1.44,22.8]}

We can call addDataToHist() many time to add more data to the histogram:

octave> hgrm = addDataToHist(hgrm, normrnd(10.7, 3.42, [1e4, 1]))
hgrm = {histogram: count=20000, range=[-1.86,24.05]}
octave> hgrm = addDataToHist(hgrm, normrnd(10.7, 3.42, [1e4, 1]))
hgrm = {histogram: count=30000, range=[-2.05,24.05]}
octave> hgrm = addDataToHist(hgrm, normrnd(10.7, 3.42, [1e4, 1]))
hgrm = {histogram: count=40000, range=[-2.42,24.05]}
octave> hgrm = addDataToHist(hgrm, normrnd(10.7, 3.42, [1e4, 1]))
hgrm = {histogram: count=50000, range=[-2.42,24.05]}

We can now compute some basic statistical properties of the data, e.g.:

The histogram can also be plotted:

octave> graphics_toolkit gnuplot
octave> plotHist(hgrm, "b");
ezprint("Hist-plot-1.png", "width", 180, "fontsize", 6);
Hist-plot-1

The bin size can be changed by resampling the histogram to a coarser bin size:

octave> plotHist(resampleHist(hgrm, -5:0.2:25), "r");
octave> ezprint("Hist-plot-2.png", "width", 180, "fontsize", 6);
Hist-plot-2

Histograms can be transformed by an arbitrary function. Here we plot the histogram of the squares of the samples:

octave> plotHist(resampleHist(transformHist(hgrm, @(x) x.^2), -10:10:650), "k");
octave> ezprint("Hist-plot-3.png", "width", 180, "fontsize", 6);
Hist-plot-3

Further histogram functions are documented in the histograms and histograms/@Hist directories.


Previous: , Up: General-Purpose Functions   [Contents][Index]