function h = qqplot(x,y,pvec)
%QQPLOT Displays an empirical quantile-quantile plot.
%   QQPLOT(X,Y) makes an empirical QQ-plot of the quantiles of
%   the data set X versus the quantiles of the data set Y.
%   H = QQPLOT(X,Y,PVEC) allows you to specify the plotted quantiles in 
%   the vector PVEC. H is a handle to the plotted lines. 
%
%   The default quantiles are those of the smaller data set.
%
%   The purpose of the quantile-quantile plot is to determine whether 
%   the samples in X and Y come from the same distribution type (parameter
%   values may be different.) If the samples do come from the same
%   distribution, the plot will be linear.  

%   Copyright (c) 1993-96 by The MathWorks, Inc.
%   $Revision: 2.3 $  $Date: 1996/02/13 23:32:43 $

if nargin < 3
    n=min(length(x),length(y));
    pvec=100*((1:n) - 0.5) ./ n;
end

if length(x) == n
    xx = sort(x);
else
    xx = prctile(x,pvec);
end
if length(y) == n
    yy = sort(y);
else
    yy=prctile(y,pvec);
end

q1x = prctile(x,25);
q3x = prctile(x,75);
q1y = prctile(y,25);
q3y = prctile(y,75);
qx = [q1x; q3x];
qy = [q1y; q3y];


dx = q3x - q1x;
dy = q3y - q1y;
slope = dy./dx;
centerx = (q1x + q3x)/2;
centery = (q1y + q3y)/2;
maxx = max(x);
minx = min(x);
maxy = centery + slope.*(maxx - centerx);
miny = centery - slope.*(centerx - minx);

mx = [minx; maxx];
my = [miny; maxy];


h = plot(xx,yy,'+',qx,qy,'-',mx,my,'-.');
set(gca,'XLabel',text(0,0,'X Quantiles'));
set(gca,'YLabel',text(0,0,'Y Quantiles'));
