Function File: [x_bin y_bin w_bin n_bin] = bin_values(x, y, k)

Average values over ranges of one variable
Given x (size n*1) and y (n*m), this function splits the range of x into up to k intervals (bins) containing approximately equal numbers of elements, and for each part of the range computes the mean of y.

Any NaN values are removed.

Useful for detecting possible nonlinear dependence of y on x and as a preprocessor for spline fitting. E.g., to make a plot of the average behavior of y versus x: errorbar(x_bin, y_bin, 1 ./ sqrt(w_bin)); grid on

Inputs:
x: n*1 real array
y: n*m array of values at the coordinates x
k: Desired number of bins, floor(sqrt(n)) by default

Outputs:
x_bin, y_bin: Mean values by bin (ordered by increasing x)
w_bin: Weights (inverse standard error of each element in y_bin; note: will be NaN or Inf where n_bin = 1)
n_bin: Number of elements of x per bin

See also: csaps, dedup.

Package: splines