Home > atmlab > graphs > histogram.m

histogram

PURPOSE ^

HISTOGRAM Histogram.

SYNOPSIS ^

function n = histogram(y,x)

DESCRIPTION ^

 HISTOGRAM  Histogram.

   Plots a histogram, defined by bin edges, rather than bin centers.

   The data given in y is counted into bins given in x. A special first
   and last bin are added. They contain additionally all data
   outside of the histogram on the respective side.

 FORMAT   n = histogram(y,x)
        
 OUT   n          Occurence count in each bin.
 IN    y          A vector of data to analyze.
       x          A vector of bin edges. There will be length(x)+1
                  bins, due to the two special bins for outsiders.

 2006-02-24   Created by Stefan Buehler

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

histogram.m

SOURCE CODE ^

0001 % HISTOGRAM  Histogram.
0002 %
0003 %   Plots a histogram, defined by bin edges, rather than bin centers.
0004 %
0005 %   The data given in y is counted into bins given in x. A special first
0006 %   and last bin are added. They contain additionally all data
0007 %   outside of the histogram on the respective side.
0008 %
0009 % FORMAT   n = histogram(y,x)
0010 %
0011 % OUT   n          Occurence count in each bin.
0012 % IN    y          A vector of data to analyze.
0013 %       x          A vector of bin edges. There will be length(x)+1
0014 %                  bins, due to the two special bins for outsiders.
0015 %
0016 % 2006-02-24   Created by Stefan Buehler
0017 
0018 function n = histogram(y,x)
0019 
0020 % Prepare the bins for the histc function, to collect also
0021 % outsiders:
0022 
0023 bins = [-inf,x,inf];
0024 
0025 n = histc(y,bins);
0026 
0027 % Safety check: The last bin must be empty, since histc stores
0028 % there all data not accounted for in any other bin, and we have
0029 % included -inf and inf:
0030 if n(end)~=0
0031   error('Unexpected behaviour of histc');
0032 else
0033   n = n(1:end-1);
0034 end
0035 
0036 % The bar function interprets indices as bar centers, not edges. We
0037 % have to generate these from our bin edges. I'm sure there is a
0038 % more Matlabish way to do this than the for loop...
0039 for i=1:length(x)-1
0040   l(i) = ( x(i) + x(i+1) ) / 2.0;
0041 end
0042 % Special treatment for first and last bin:
0043 first = x(1)   - ( l(1)   - x(1)   );
0044 last  = x(end) + ( x(end) - l(end) );
0045 l = [first,l,last];
0046 
0047 %length(bins)
0048 %length(l)
0049 %length(n)
0050 
0051 % Draw the bar plot:
0052 bar(l,n);
0053

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005