function histogram(x,bins,ptype)
% usage histogram(x,bins)
%   x = data matrix (rows are observations, columns are different series)
%   histogram will plot a histogram for each series for comparison
%   * marks the sample mean
nseries = size(x,2);
n = size(x,1);

if(nargin==2)
    ptype = 1;
end
if (n==1)
    x = x';
    n = size(x,1);
    nseries = 1;
end
rmax = max(max(x));
gmax = rmax;
rmin = min(min(x));
gmin = rmin;

delta = (rmax-rmin)/(bins);
rmin = rmin + delta/2;
rmax = rmax - delta/2;

xpt = rmin;
i = 1;
while (xpt < rmax+delta/4) 
    center(i) = xpt;
    i = i+1;
    xpt = xpt+delta;
end
ncenter = i-1;
nbins = zeros(ncenter,nseries);

for i=1:nseries
    [nbins(:,i)] = hist(x(:,i),center)';
end

mheight = max(max(nbins));

if(ptype == 1)
hold off;
for i=1:nseries
    subplot(nseries,1,i);
    bar(center,nbins(:,i),'r');
    hold on;
    h = text(mean(x(:,i)),mheight/2,'*');
    set(h,'FontSize',24);
    set(h,'Color','k');
    axis([gmin gmax 0 mheight]);

end  
end
if(ptype==2)
    hold off;
 for i=1:nseries
     zz (:,i) = nbins(:,i);
 end
 yy = center;
 bar3(yy,zz);
end 
if(ptype == 3)
    bar(center,nbins,1);
end

