function [best,names] = bestByTestTrial(CV,printp)

if nargin < 2
printp = 1;
end

names = {CV.cvresultsNames{:} 'Val Frac Corr'};% 'Test RMSE'};

best = [];
%% Find mean of cv repetitions.
nreps = CV.validate.repetitions;

mat = CV.cvresults(:,1:end-nreps);
z=CV.cvresults(:,end-nreps+1:end);
mat = [mat mean(CV.cvresults(:,end-nreps+1:end),2)];

for testtrial = unique(mat(:,1))'  %first column is test trials
best1 = [];
  submat = mat(mat(:,1)==testtrial,:);
  [junk,index]=max(submat(:,end));
  for i = 1:size(mat,2)
    if printp
      if i==size(mat,2)
	fprintf(1,'%s %5.1f ',names{i},100*submat(index,i));
      else
	fprintf(1,'%s %g ',names{i},submat(index,i));
      end
    end
    best1 = [best1 submat(index,i)];
  end
  testres = CV.testResults{testtrial}{6};
  if printp, fprintf(1,'Test Frac Corr %5.1f',testres*100); end
best1 = [best1 CV.testResults{testtrial}{6}];
best = [best; best1];
  if printp, fprintf(1,'\n'); end;
end

fprintf(1,'Check below to see if best parameters agree\n');
summarizeTestResults(CV);
