function [confint,five,datamean,stat]=ConfInt2MeansBootstrap(samp1,samp2,N,alpha) 
         

if nargin<3 N=20000; end

% default is 20000 resamples.

if nargin<4 alpha=0.95; end

% default is a 95% confidence interval.

% This function takes N bootstrap samples (samples with replacement) of the two original data samples and stores the calculation of the desired statistic (the difference of the resample means) in a sorted vector of values. 

% It also gives (by default alpha=0.95) the endpoints of the interval for which 2.5% of the resample mean differences are to the left and 2.5% are to the right, a 95% confidence interval for the difference of these two means.

% This is called a bootstrap percentile confidence interval and is not corrected for skewness or bias, but it is ``first-order correct'' and provides a useful approximation.

% samp1=[141,156.5,162,159,157,143.5,158,140]
% samp2=[142,150,148.5,138.5,161,153,145,147,158.5,160.5,167.5,155,137]

datamean=mean(samp1)-mean(samp2);

rng('shuffle') %assign time-based seed to random number generator

n=length(samp1); 
m=length(samp2);
     
for i=1:N 
    
choose1=randi(n,[1 n]);
choose2=randi(m,[1 m]);
    
resample1=samp1(choose1);
resample2=samp2(choose2);

stat(i)=mean(resample1)-mean(resample2); 

end;

stat=sort(stat);

left=stat(round((1-alpha)/2*N));
right=stat(round((1+alpha)/2*N));

confint=[left, right];

figure(63);
    
hist(stat,100);
  hold on
  plot([left left right right],[0 N/40 N/40 0])
    plot([datamean datamean],[0,N/30])
hold off
five=fivenum(stat);


end

% The following sub-function calculates the 5-number summary statistics for a list.

function A=fivenum(L)

if length(L)<5,error('There must be at least 5 data points'),end

L=sort(L);

med=median(L);
minimum=min(L);
maximum=max(L);

if length(L)/2==ceil(length(L)/2) 

q1=median(L(1:(length(L)/2)));
q3=median(L((length(L)/2+1):length(L)));

else

q1=median( L(      1:((length(L)-1)/2)   )  );
q3=median(   L(   ((length(L)+3)/2):length(L)    )  );

end

A=[minimum, q1, med, q3, maximum];
end