Home > atmlab > math > binning.m

binning

PURPOSE ^

BINNING Simple "binning" of data

SYNOPSIS ^

function [Yb,dx] = binning( x_new, x,Y )

DESCRIPTION ^

 BINNING   Simple "binning" of data

  Binning means here that the original data Y will be binned
  on a new grid in such a way that the binned data 
  Yb(x_i,y_i) will corresponds to weighted average 
  of Y over the intervall 
  [ x_i-(x_i-x_{i-1})/2, x_i+(x_{i+1}-x_{i})/2 ]
  If the original data are not defined outside
  the ranges of the new grid, the original data
  are extrapolated, in order to deal with
  with the end points.
  The binning is performed in one dimension. 
  
 FORMAT   [Yb,dx] = binning( x_new, x, Y )
        
 OUT   Yb      Binned data (vector or matrix).
       dx      The size of each bin of x_new
 IN    x_new   The new grid to bin the data on.
       x       Original x-grid.
       Y       Data to be binned.

 See also: bin, binning_fast, bin_nd

 2007-11-12   Created by Bengt Rydberg.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

binning.m

SOURCE CODE ^

0001 % BINNING   Simple "binning" of data
0002 %
0003 %  Binning means here that the original data Y will be binned
0004 %  on a new grid in such a way that the binned data
0005 %  Yb(x_i,y_i) will corresponds to weighted average
0006 %  of Y over the intervall
0007 %  [ x_i-(x_i-x_{i-1})/2, x_i+(x_{i+1}-x_{i})/2 ]
0008 %  If the original data are not defined outside
0009 %  the ranges of the new grid, the original data
0010 %  are extrapolated, in order to deal with
0011 %  with the end points.
0012 %  The binning is performed in one dimension.
0013 %
0014 % FORMAT   [Yb,dx] = binning( x_new, x, Y )
0015 %
0016 % OUT   Yb      Binned data (vector or matrix).
0017 %       dx      The size of each bin of x_new
0018 % IN    x_new   The new grid to bin the data on.
0019 %       x       Original x-grid.
0020 %       Y       Data to be binned.
0021 %
0022 % See also: bin, binning_fast, bin_nd
0023 %
0024 % 2007-11-12   Created by Bengt Rydberg.
0025 
0026 function [Yb,dx] = binning( x_new, x,Y )
0027 
0028 if ~isvector(x_new) & ~isvector(x)
0029    error('x_new and x must be vectors.')
0030 end
0031 
0032 if isvector(Y)
0033   Y=vec2col(Y);
0034 end
0035 
0036 if length(x)~=size(Y,1)
0037    error('Mismatch in size between Y and x.')
0038 end
0039 
0040 if min(x_new)<min(x) | max(x_new)>max(x)
0041    %warning('x_new is outside the range of x')
0042 end
0043 
0044 x_new=vec2col(x_new);
0045 x=vec2col(x);
0046 
0047 %create a grid with end points of x_new
0048 x_new2=zeros(length(x_new)+1,1);
0049 x_new2(1)=x_new(1)-(x_new(2)-x_new(1))/2;
0050 x_new2(end)=x_new(end)+(x_new(end)-x_new(end-1))/2;       
0051 x_new2(2:end-1)=x_new(1:end-1)+(x_new(2:end)-x_new(1:end-1))/2;
0052 
0053 %combine the end points with the centre points for each bin
0054 x_new2=union(x_new2,x_new);
0055 %combine the update grid with the original grid
0056 x_new3=union(x_new2,x);
0057 
0058 ny = size( Y, 2 );
0059 
0060 %interpolate data onto the combined grid
0061 Y1=zeros(length(x_new3),ny);
0062 for i=1:ny
0063     Y1(:,i)=interp1( x ,Y(:,i), x_new3,'linear','extrap');
0064 end
0065 
0066 xlen=length(x_new);
0067 Yb=zeros(length(x_new),ny);
0068 dx=zeros(length(x_new),1);
0069 for i=1:xlen
0070     j1=2*(i-1)+1;
0071     j2=2*i+1;
0072     ind=find(x_new3>=x_new2(j1) & x_new3<=x_new2(j2));
0073     %the size for matching indices
0074     dx3=x_new3(ind(2:end))-x_new3( ind(1:end-1));
0075     %the size of each bin of x_new
0076     dx(i)=x_new2(j2)-x_new2(j1);
0077     Yb(i,1:ny)=sum((( Y1(ind(1:end-1),:)+ Y1(ind(2:end),:))/2.*...
0078             [dx3/dx(i)*ones(1,ny)]));
0079 end
0080

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005