function [teams1,nteams1,exitcode]=wctiebreaker(teams0,nteams0,a,conf,div,wltpct,cwltpct,dwltpct,sov,sos) % Copyright 2006 by William S. Krasker % Given a set of nteams0 teams from the same conference but different divisions, whose indices are % in the array teams0, this function runs through a sequence of criteria until one is found for % which not all nteams0 teams are equally good. The function then returns the number nteams1 of teams % that are tied for the best according to that criterion, the array teams1 containing their indices, % and the exitcode showing the criterion that achieved the separation. % % The criteria are: % % 0. overall won-lost-tied percentage, % 1. head-to-head sweep (applicable only if one team has defeated each of the others or if one team % has lost to each of the others), % 2. won-lost-tied percentage in the conference, % 3. won-lost-tied percentage in games against common opponents (minimum of four), % 4. strength of victory, and % 5. strength of schedule. % % (Criteria 1 through 5 are called tiebreakers.) If none of these criteria differentiates among the % teams, then the function arbitrarily eliminates the last team. teams1=zeros(4,1); for ii=1:1 %-------------------------------------------------- % find best win percentage v=zeros(4,1); x=0; for i=1:nteams0 x=max(x,wltpct(teams0(i))); end % find teams with best win percentage k=0; for i=1:nteams0 if wltpct(teams0(i))==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on win percentage nteams1=k; teams1(1:k)=v(1:k); exitcode=0; break end %-------------------------------------------------- % see if there is a head-to-head sweep y=zeros(32,32); for j=1:256 if div(a(j,1))~=div(a(j,2)) if a(j,3)==1 y(a(j,2),a(j,1))= 1; y(a(j,1),a(j,2))=-1; elseif a(j,3)==0 y(a(j,2),a(j,1))=-1; y(a(j,1),a(j,2))= 1; end end end % kth column of y now contains 1 for a win by team k, -1 for a loss by team k Y=zeros(nteams0,nteams0); % submatrix of y corresponding to the nteams0 teams for i=1:nteams0 for k=1:nteams0 Y(k,i)=y(teams0(k),teams0(i)); end end sweep=0; for i=1:nteams0 if sum(Y(:,i))==nteams0-1 sweep=teams0(i); istar=i; end end if sweep>0 % team teams0(i) swept nteams1=1; teams1(1)=teams0(istar); exitcode=1; break end sweep=0; for i=1:nteams0 if sum(Y(:,i))==-(nteams0-1) sweep=teams0(i); end end if sweep>0 % team teams0(i) was swept nteams1=nteams0-1; r=0; for i=1:nteams0 if teams0(i)~=sweep r=r+1; teams1(r)=teams0(i); end end exitcode=1; break end %-------------------------------------------------- % find best win percentage in the conference v=zeros(4,1); x=0; for i=1:nteams0 x=max(x,cwltpct(teams0(i))); end % find teams with best win percentage in the conference k=0; for i=1:nteams0 if cwltpct(teams0(i))==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on win percentage in the conference nteams1=k; teams1(1:k)=v(1:k); exitcode=2; break end %-------------------------------------------------- % find common opponents y=zeros(32,32); for j=1:256 y(a(j,1),a(j,2))=y(a(j,1),a(j,2))+1; y(a(j,2),a(j,1))=y(a(j,2),a(j,1))+1; end z=ones(32,1); p=zeros(32,1); for i=1:nteams0 z=z.*y(:,teams0(i)); % positive elements of z correspond to common opponents p(teams0(i))=1; % positive elements of p correspond to the teams under consideration end % find number of games against common opponents commongames=zeros(nteams0,1); for i=1:32 if z(i)>0 for k=1:nteams0 commongames(k)=commongames(k)+y(i,teams0(k)); end end end if min(commongames)>=4 % if at least 4 games against common opponents, do the calculations % find win percentage against common opponents w=zeros(32,1); num=zeros(32,1); for j=1:256 if (p(a(j,1))>0) & (z(a(j,2))>0) num(a(j,1))=num(a(j,1))+1; if a(j,3)==1 w(a(j,1))=w(a(j,1))+1; elseif a(j,3)==-1 w(a(j,1))=w(a(j,1))+0.5; end elseif (z(a(j,1))>0) & (p(a(j,2))>0) num(a(j,2))=num(a(j,2))+1; if a(j,3)==0 w(a(j,2))=w(a(j,2))+1; elseif a(j,3)==-1 w(a(j,2))=w(a(j,2))+0.5; end end end q=zeros(4,1); for i=1:nteams0 q(i)=w(teams0(i))/num(teams0(i)); end % find best win percentage against common opponents x=max(q(1:nteams0)); % find teams with best win percentage against common opponents k=0; v=zeros(4,1); for i=1:nteams0 if q(i)==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on win percentage against common opponents nteams1=k; teams1(1:k)=v(1:k); exitcode=3; break end end %-------------------------------------------------- % find best strength of victory v=zeros(4,1); x=0; for i=1:nteams0 x=max(x,sov(teams0(i))); end % find teams with best strength of victory k=0; for i=1:nteams0 if sov(teams0(i))==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on strength of victory nteams1=k; teams1(1:k)=v(1:k); exitcode=4; break end %-------------------------------------------------- % find best strength of schedule v=zeros(4,1); x=0; for i=1:nteams0 x=max(x,sos(teams0(i))); end % find teams with best strength of schedule k=0; for i=1:nteams0 if sos(teams0(i))==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on strength of schedule nteams1=k; teams1(1:k)=v(1:k); exitcode=5; break end %-------------------------------------------------- % if all else fails, just drop the last team on the list nteams1=nteams0-1; teams1=teams0; exitcode=6; end