histogram_data_2d_sample, a MATLAB code which demonstrates how to construct a Probability Density Function (PDF) from a table of sample data, and then to use that PDF to create new samples.

The program presented here is hard-wired to handle a specific problem. However, the ideas used in the program are easily extended to other regions and other dimensions.

For the problem given here, we assume we have sample values of a function F(X,Y) for each subregion of a region. These values might actually represent population counts, a density, the integral of some function over the subregion, or simply an abstract function. We implicitly assumed that all the values are positive.

The particular region studied here is the unit square, which has been broken down into a 20x20 array of equal subsquares.

If we normalize by the sum of the data values, the result is a PDF associated with each subregion. By assigning an arbitrary order to the subregions, we can add the PDF values up to the given subregion to get a CDF (cumulative density function) for that subregion. Now given an arbitrary random value U, we can locate the subregion whose CDF value just exceeds U. Choosing a random point within this subregion gives us the sample point. If we choose many such sample points, the statistics for this sample will tend to the discrete PDF that we defined from the data we were given.


The computer code and data files described and made available on this web page are distributed under the MIT license


histogram_data_2d_sample is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version.

Related Data and Programs:

fem1d_sample, a MATLAB code which samples a scalar or vector finite element function of one variable, defined by FEM files, returning interpolated values at the sample points.

fem2d_sample, a MATLAB code which evaluates a finite element function defined on an order 3 or order 6 triangulation.


histogram_pdf_2d_sample, a MATLAB code which demonstrates how uniform sampling of a 2D region with respect to some known Probability Density Function (PDF) can be approximated by decomposing the region into rectangles, approximating the PDF by a piecewise constant function, constructing a histogram for the CDF, and then sampling.

prob, a MATLAB code which evaluates and inverts a number of probabilistic distributions.

random_data, a MATLAB code which generates sample points for various probability distributions, spatial dimensions, and geometries;

rejection_sample, a MATLAB code which demonstrates acceptance/rejection sampling.

walker_sample, a MATLAB code which efficiently samples a discrete probability vector using Walker sampling.

Source Code:

Last modified on 30 January 2019.