Home > matutils > group.m

group

PURPOSE ^

function [s,e]=group(ind)

SYNOPSIS ^

function [s,e]=group(ind)

DESCRIPTION ^

 function [s,e]=group(ind)

 find the start (s) and stop (e) index of groups of ones
 in vector ind.  does not loop over entire vector!

 for example:
   ind=[0 1 1 0 1];
   s=[1 5]
   e=[2 5]

 Michael Loh

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [s,e]=group(ind)
0002 
0003 % function [s,e]=group(ind)
0004 %
0005 % find the start (s) and stop (e) index of groups of ones
0006 % in vector ind.  does not loop over entire vector!
0007 %
0008 % for example:
0009 %   ind=[0 1 1 0 1];
0010 %   s=[1 5]
0011 %   e=[2 5]
0012 %
0013 % Michael Loh
0014 
0015 s=[];
0016 e=[];
0017 
0018 f=find(ind==1);
0019 cnt=1;
0020 if (~isempty(f))
0021   dif=diff(f);
0022   ff=find(dif>1);    
0023   if (isempty(ff))
0024     s(cnt)=f(1);
0025     e(cnt)=last(f);
0026     cnt=cnt+1;
0027   else
0028     for j=1:(length(ff)+1)
0029       if (j==1)
0030     s(cnt)=f(1);
0031     e(cnt)=f(ff(j));
0032       elseif (j==(length(ff)+1))
0033     s(cnt)=f(ff(j-1)+1);
0034     e(cnt)=last(f);
0035       else
0036     s(cnt)=f(ff(j-1)+1);
0037     e(cnt)=f(ff(j));
0038       end
0039       cnt=cnt+1;
0040     end
0041   end
0042 end
0043 
0044 s=sort(s);
0045 e=sort(e);
0046

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