function [bestelemwise,bestseqwise] = choosebestscore(scores,starts,tiebreaker) % CHOOSEBESTSCORE : chooses bestseqwise score % % [bestelemwise,bestseqwise] = choosebestseqwisescore(scores,starts,tiebreaker) % % Suppose there are K sequences with a total of N elements % The N elements have scores (from some previous classifier) in scores % The elements of the k-th sequence are in % starts(k):starts(k+1)-1, if k == 1 to K-1 % or starts(K):N if k == K % % scores is a N-element vector of scores % starts is a K-element vector of start positions % (it is a monotone increasing subvector of [1:N]) % tiebreaker is one of the strings % 'first' : in case of ties, choose the first % 'last' : in case of ties, choose the last % 'rand' : in case of ties, choose something randomly % % bestseqwise is a K x 1 vector whose k-th element a=bestseqwise(k) is between 1 and n inclusive % (where n is the length of the k-th sequence) and a-th score in that k-th sequence is highest % % bestelemewise is a N x 1 binary vector whose n-th element is 1 if the n-th element had the % highest score in its sequence % % Example: % % scores = [1.1 0.5 1.1 0.2 -0.4] % starts = [1 4] % tiebreaker = 'last' % % This example has two sequences, % the first sequence has scores [1.1 0.5 1.1] and % the second sequence has scores [0.2 -0.4] % % The output is % % bestseqwise = [3 1] % bestelemwise = [0 0 1 1 0] if (size(starts,1)==1),starts=starts';end % now is col vector K = length(starts); N = length(scores); starts(K+1)=N+1; bestseqwise=zeros(K,1); for k=1:K a = starts(k); b = starts(k+1)-1; x = scores(a:b); f = find(x == max(x)); if (length(f)==1) bestseqwise(k)=f; else if strcmp(tiebreaker ,'first') bestseqwise(k)=f(1); elseif strcmp(tiebreaker ,'last') bestseqwise(k)=f(end); elseif strcmp(tiebreaker ,'rand') bestseqwise(k) = max(1,ceil(rand*length(f))); else error(sprintf('unknown tiebreaker %s',tiebreaker)); end end end bestelemwise = zeros(N,1); bestelemwise(starts(1:K)-1+bestseqwise)=1;