function [out,nruns]=runs2(DATA);
% format function [out,nruns]=runs2(DATA)
% input: DATA: a row or column vector
% output:     
%        out matrix:  column 1 length of runs (1,2,... max run length)
%                     column 2 number of runs of each length in col 1
%        nruns:       [Totalruns, posruns, negruns, tiedvalues]
%                     posruns: number of positive runs
%                     negruns: number of negative runs
%                     tiedvalues: elements deleted from rank calcs
%                                 because of exact ties in DATA
% calls the resample toolbox runs.m
% written by E. Gallagher, 5/27/03, revised 9/13/2010
% Eugene.Gallagher@umb.edu for EEOS601
[r,c]=size(DATA);
if c>r
    DATA=DATA';  % change row vector to column vector
    r=c
end
if r<3; disp('Vector must have more than 2 values'); return;end
% Find the tied values in the series, and delete redundant elements
   % provides the positive and negative differences:
   df=diff(DATA);
   % find the differences not equal to zero, make 1
   df=df~=0;
   j=[1; df.*[2:r]']; % change indices of redundant elements in
                      % tied groups to zero
   k=j(find(j>0));    % find the indices,k, just for the 1st untied vals
   untiedd=DATA(k);n2=length(untiedd); % create the untied vector
%
% Now, find the runs using the differencing function in Matlab, diff:
   ddiff=diff(untiedd); % create the vector of differences
   signd=ddiff>0; % change positive diffs to 1, negative diffs to 0
% Use the runs.m file from the resample toolbox to find runs & lengths
   [len,vals]=runs(signd); % runs.m from the resample toolbox
   nruns=length(len);
    maxl=max(len); % find the largest run size
% create matrix of run lengths(1st column) and observed number of runs
% Using the built in Matlab function histc, designed for creating histogram
% bins, but very useful for adding categories.
% built in matlab function, histc
sizeruns=[1:maxl]';
N=histc(len,sizeruns)';
% create the output matrix out
out=[sizeruns N];

% Find the positive runs by deleting extra 1's see above;
df=diff(signd);
% find the differences not equal to zero, make 1
df=df~=0;
lsd=length(signd);
j=[1; df.*[2:lsd]']; % change index of later ties to zero
k=j(find(j>0)); % find the indices just for the 1st untied vals
posdiffs=signd(k);posruns=sum(posdiffs);
Totinruns=sum(out(:,1).*out(:,2));
% Number of elements deleted due to exact ties:
tiedd=r-Totinruns-1;
nruns=[nruns posruns nruns-posruns tiedd];

function [len, vals] = runs(data)
% RUNS --- finds runs (sequences of repeated values)
% runs(data) 
% gives the lengths of runs in the data set.
%
% [len, vals] = runs(data) 
% gives both the length of the runs and the value of that run.
% from the Resampling toolbox

% (c) 1998-9 by Daniel T. Kaplan, All Rights Reserved
% Version 1.0

% put the data into row format
[r,c] = size(data);
if r~=1 & c~=1
  error('runs: input must be a vector.');
end

if c==1
  data = data';
end

% find the starts of the runs
inds = [1, (1+find(0 ~= diff(data)))];

len = diff([inds,length(data)+1]);
vals = data(inds);
