binToVector and binToMatrix

Provides functions that bin randomly sampled data to a reference grid for 1D and 2D data.
29 Downloads
Updated 22 Nov 2017

View License

Provides two function that bin randomly sampled data to an equispaced reference grid that is either 1D (binToVector) or 2D (binToMatrix). Should work on older versions of MATLAB.
Scales the same way as the histcounts and accumarray method on the input data-set size but seems to provide a speed up of approximately a factor of 2 and less overhead for smaller data sets, at least in my implementation.

Inspired by binXYZonXY but should provide a speed improvement (https://www.mathworks.com/matlabcentral/fileexchange/41386-binning-a-point-cloud--3d-scattered-data--in-the-x-y-plane).

yB = binToVector(x,y,xB)
[yB, yBstd] = binToVector(x,y,xB)
[yB, yBstd, bin_population] = binToVector(x,y,xB)
Calculates yB = yB(xB) which is the binned version of y=y(x) using the
bin centers of the equispaced reference grid xB. The first and last bins
also include data half a bin width outside of the reference grid.
If the bin contains no elements, NaN is returned for that bin. Nan
elements in y are ignored. The input data x can be randomly sampled.

zB = binToMatrix(x,y,z,xB,yB)
[zB, zBstd] = binToMatrix(x,y,z,xB,yB)
[zB, zBstd, bin_population] = binToMatrix(x,y,z,xB,yB)
Calculates zB = zB(xB,yB) which is the binned version of the randomly
sampled function z=z(x,y). It uses the bin centers of the equispaced
reference grid xB,yB (created with meshgrid). The first and last bins
in each direction also include data half a bin width outside of the
reference grid. If the bin contains no elements, NaN is returned for
that bin. Nan elements in z are ignored.

Written by Karl Berggren, Royal Institute of Technology, Stockholm

Cite As

Karl Berggren (2024). binToVector and binToMatrix (https://www.mathworks.com/matlabcentral/fileexchange/65154-bintovector-and-bintomatrix), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2015b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0