Home > atmlab > math > bin_nd.m

bin_nd

PURPOSE ^

BIN_ND Bin data in arbitrary number of dimensions

SYNOPSIS ^

function values = bin_nd(binners, bins, varargin)

DESCRIPTION ^

 BIN_ND Bin data in arbitrary number of dimensions

 Generic function to bin data in any number of dimensions.
 For a vector of 2726996 elements, takes about 3 seconds (binning_fast
 takes more than 60 seconds).
 Note: still in beta, need to check corner cases. Not ready for production
 use. Results slightly different from binning_fast, probably due to
 boundary conditions and such.
 Note: (so far) only returns indices, does not actually bin data yet.

 FORMAT

   indices = bin_nd({lat, lon, ...}, {bins_lat, bins_lon, ...})

 IN

   - cell with axes that data is binned at, for example lat, lon.
   - cell with bins for each of those

 OUT

   n-dimensional cell array with binned indices

 EXAMPLE

   indces = bin_nd({lats, lons}, {lat_bins, lon_bins})

 See also: bin, binning_fast

 Gerrit Holl, March 2013

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

bin_nd.m

SOURCE CODE ^

0001 function values = bin_nd(binners, bins, varargin)
0002 % BIN_ND Bin data in arbitrary number of dimensions
0003 %
0004 % Generic function to bin data in any number of dimensions.
0005 % For a vector of 2726996 elements, takes about 3 seconds (binning_fast
0006 % takes more than 60 seconds).
0007 % Note: still in beta, need to check corner cases. Not ready for production
0008 % use. Results slightly different from binning_fast, probably due to
0009 % boundary conditions and such.
0010 % Note: (so far) only returns indices, does not actually bin data yet.
0011 %
0012 % FORMAT
0013 %
0014 %   indices = bin_nd({lat, lon, ...}, {bins_lat, bins_lon, ...})
0015 %
0016 % IN
0017 %
0018 %   - cell with axes that data is binned at, for example lat, lon.
0019 %   - cell with bins for each of those
0020 %
0021 % OUT
0022 %
0023 %   n-dimensional cell array with binned indices
0024 %
0025 % EXAMPLE
0026 %
0027 %   indces = bin_nd({lats, lons}, {lat_bins, lon_bins})
0028 %
0029 % See also: bin, binning_fast
0030 %
0031 % Gerrit Holl, March 2013
0032 
0033 % $Id: bin_nd.m 8259 2013-03-04 19:07:59Z gerrit $
0034 
0035 errid = ['atmlab:' mfilename];
0036 
0037 if length(binners) ~= length(bins)
0038     error(errid, 'Inconsistent lengths: %d binners, %s bins', ...
0039           length(binners), length(bins));
0040 end
0041 
0042 
0043 data = optargs(varargin, {vec2col(uint32(1:length(binners{1})))});
0044 nd = length(binners);
0045 
0046 if nd > 1
0047     innerbinned = bin(binners{end}, data, bins{end});
0048     values = cell(length(innerbinned), 1);
0049     for i = 1:length(innerbinned)
0050         binners_selection = cellfun(@(X)(X(innerbinned{i})), binners(1:end-1), 'UniformOutput', false);
0051         values{i} = bin_nd(binners_selection, bins(1:end-1), innerbinned{i});
0052     end
0053 else
0054     values = bin(binners{1}, data, bins{1});
0055 end
0056 
0057 if length(bins) > 1
0058     values = reshape(horzcat(values{:}), cellfun(@length, bins));
0059 end
0060 end

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