function [h,div]=HistogramCount(varargin)

%HistogramCount: represents the numbers of elements of multiple sample sets, piled
%
% [h,div]=HistogramCount(sampleset1[,sampleset2,...])
%
% sampleset#: each of the sample sets
% h(:,i): number of elements in each sample sets in the calculated divisions
% div:  division intervals of the histogram

% APRENDIZAJE AUTOMÁTICO (G. Pajares y J. M. de la Cruz, Eds.)
% Editorial: RA-MA
% Capítulo 7 y 8: Monte Carlo I y II
% Autores: Eva Besada-Portas, Sergey M. Plis

hm=length(varargin);

%Calculame the min and max value of all the datasets
mins=min(varargin{1});
maxs=max(varargin{1});
for i=2:hm
    mins=min([mins,varargin{i}]);
    maxs=max([maxs,varargin{i}]);
end

%Find the intervals in the histogram
div=linspace(mins,maxs,100);
step=div(end)-div(end-1);
div=[div(1)-step,div,div(end)+step];

%Accoding with the division, count the elements in each inteval
for i=1:hm
    h(:,i)=histc(varargin{i},div);
end


%Represent the histogram
hr=bar(div+step/2,h,1,'stacked'); %We have to add, because in bar, points represent centers. And we have used histc, where points are intervals
colormap(autumn);

%Special code for using the final colours that appear in the text when two
%sets of samples are used.
if length(hr)==2
    set(hr(1),'FaceColor','r')
    set(hr(2),'FaceColor',0.8*[1 1 0])
    set(hr(2),'EdgeColor',0.8*[1 1 0])
end

 