Home > air_sea > binave.m

binave

PURPOSE ^

BINAVE: averages vector data in bins of length r.

SYNOPSIS ^

function bindat = binave(data,r)

DESCRIPTION ^

 BINAVE: averages vector data in bins of length r.
 bindat=BINAVE(data,r) computes an average vector of the vector 
 data in bins of length r.  The last bin may be the average of 
 less than r elements. Useful for computing daily average time 
 series (with r=24 for hourly data).

 INPUT:   data - data vector
          r - number of elements in bin to be averaged

 OUTPUT:  bindat - bin-averaged vector

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function bindat = binave(data,r)
0002 % BINAVE: averages vector data in bins of length r.
0003 % bindat=BINAVE(data,r) computes an average vector of the vector
0004 % data in bins of length r.  The last bin may be the average of
0005 % less than r elements. Useful for computing daily average time
0006 % series (with r=24 for hourly data).
0007 %
0008 % INPUT:   data - data vector
0009 %          r - number of elements in bin to be averaged
0010 %
0011 % OUTPUT:  bindat - bin-averaged vector
0012 
0013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0014 % 3/8/97: version 1.0
0015 % 9/19/98: version 1.1 (vectorized by RP)
0016 % 8/5/99: version 2.0
0017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0018 
0019 % check input
0020 if nargin < 2
0021    error('Not enough input arguments.')
0022 end
0023 if abs(r-fix(r)) > eps
0024    error('Bin size R must be a positive integer.')
0025 end
0026 if fix(r) == 1
0027     bindat = data;
0028     return
0029 end
0030 if r <= 0
0031     error('Bin size R must be a positive integer.')
0032 end
0033 
0034 [N,M]=size(data);
0035 
0036 % compute bin averaged series
0037 l = length(data)/r;
0038 l = fix(l);
0039 bindat = mean(reshape(data(1:l*r),r,l));
0040 
0041 if length(data)>l*r,
0042  bindat=[bindat,mean(data(l*r+1:end))];
0043 end;
0044 
0045 if N~=1
0046   bindat=bindat';
0047 end
0048 
0049 
0050

Generated on Thu 26-Apr-2007 12:23:13 by m2html © 2003