Weighted Data Binning [wbin]

Weighted data binning.
454 Downloads
Updated 5 Feb 2012

View License

WBIN(X,Y,E,DX) returns the weighted mean of the elements in Y within a
bin of size DX.
X,Y,E are row or column vectors with an equal number of elements.
DX is a scalar which specifies the desired binning interval.

Input X,Y,E,DX must not contain NaN values!

E is a vector which contains the error (one sigma standard deviation)
of the elements in Y. The elements are assumed to be uncorrelated and
elements in E should be Gaussian distributed.
The weighting scheme thus assumes the standard formular: W = 1./E.^2;

Output XB,YB,EB,NE are column vectors containing binned positions,
weighted binned data points, error on weighted binned data points
(one sigma standard deviation) and number of elements used to calculate
the respective weighted binned data point.

Examples
1. Weighted bin of linear data + random distributed noise.
X = 1:1000;
E = randn(1, 1000);
Y = X + E;
DX = 10;
wbin(X,Y,E,DX);
2. Weighted bin with an equal number of elements used to calculate bin.
X = linspace(0,60,10001);
E = randn(1, numel(X));
Y = X + E;
DX = 6;
wbin(X, Y, E, DX);

Cite As

Michael Lindholm Nielsen (2024). Weighted Data Binning [wbin] (https://www.mathworks.com/matlabcentral/fileexchange/34509-weighted-data-binning-wbin), MATLAB Central File Exchange. Retrieved .

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

Inspired by: wmean

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.12.0.0

(1) Implemented stronger condition on binning interval.
(2) Now capable of ignoring NaN values in X, Y and E.

1.11.0.0

(1) Corrected weak condition on NaN error checking.
(2) Minor changes in code setup.

1.9.0.0

(1) Changed while loop to for loop due to performance issues.
(2) Corrected error in selecting the correct weights.
(3) Corrected error in calculating mean position.
(4) Corrected error in preallocation.
(5) Added change log and example 2.

1.4.0.0

(1) Corrected Binning Interval to: X <= X(1) + DX
(2) Position of bin is now calculated as a standard mean, since there is no uncertainty on the position (X).
(3) Added optional output: Number of elements used to calculate the respective bins.

1.3.0.0

(1) Corrected Binning Interval to: X <= X(1) + DX
(2) Position of bin is now calculated as a standard mean, since there is no uncertainty on the position (X).
(3) Added optional output: Number of elements used to calculate the respective bins.

1.1.0.0

Checks if X,Y,Z or DX contains NaN values.

1.0.0.0