function [d0,d1] = runlength(f)

%The matlab built-in function "diff" computes the 
%difference between successive elements. 
%The positions of the non-zero entries in the output 
%b are the positions of 1-0 or 0-1 transitions
b = diff(f);

%The matlab built-in function "find" computes the 
%position of non-zero entries
c = find(b);

%If c is empty, there was no transition, i.e. it was 
%a row of all 0 or all 1
  if(isempty(c))
     d = length(f);
  %Otherwise, another diff command gives the required runlengths   
  else
     d = [c(1) diff(c)];    
     d = [d length(f)-sum(d)];
  end
  
  %First run is always a run of 1, which maybe of length 0 
  if(f(1) == 0) d = [0 d];    
  end
  
  %Seperate out the runlengths of 0 and 1
  d1 = d(1:2:length(d));
  d0 = d(2:2:length(d));
  