Home > matutils > bin.m

bin

PURPOSE ^

function bv=bin(v,npts,dim)

SYNOPSIS ^

function bv=bin(v,npts,dim)

DESCRIPTION ^

 function bv=bin(v,npts,dim)

 bin vector by meaning npts elements of vectors along dim 
 dim defaults to one.

 if npts is two dim then that signifies the start and stop indices
 for binning

 example:
   npts=[1 5;
         8 9]
   will bin v(1:5) and v(8:9)

 Michael Loh

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function bv=bin(v,npts,dim)
0002 
0003 % function bv=bin(v,npts,dim)
0004 %
0005 % bin vector by meaning npts elements of vectors along dim
0006 % dim defaults to one.
0007 %
0008 % if npts is two dim then that signifies the start and stop indices
0009 % for binning
0010 %
0011 % example:
0012 %   npts=[1 5;
0013 %         8 9]
0014 %   will bin v(1:5) and v(8:9)
0015 %
0016 % Michael Loh
0017 
0018 if (~exist('dim'))
0019   dim=1;
0020 end
0021 
0022 v=cvec(v);
0023 
0024 if (size(npts,2)==1)
0025   if (~npts)
0026     error('Cannot bin with npts=0')
0027   end
0028   ln=ceil(length(v)/npts);
0029   
0030   for i=1:ln
0031     s=(i-1)*npts+1;
0032     if (i<ln)
0033       e=s+npts-1;
0034       bv(i,:)=nanmean(v(s:e,:),dim);
0035     else
0036       bv(i,:)=nanmean(v(s:end,:),dim);
0037     end
0038   end
0039 else
0040   for i=1:size(npts,1)
0041     s=npts(i,1);
0042     e=npts(i,2);
0043     bv(i,:)=nanmean(v(s:e,:),dim);
0044   end
0045 end
0046 
0047   
0048     
0049

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005