Home > FMAToolbox > General > Bin.m

Bin

PURPOSE ^

Bin - Assign each input value a bin number between 1 and N.

SYNOPSIS ^

function [bins,binned] = Bin(x,a,b,c)

DESCRIPTION ^

Bin - Assign each input value a bin number between 1 and N.

  Assign each input value a bin number between 1 and N. Also return the
  corresponding bin values (lower bounds), e.g. assuming that the bins
  are [0,1] [1,2] [2,3] ... the value 2.3 falls in bin #3 and the bin
  value (lower bound) is 2.

  USAGE

    % To specify bin limits and # bins
    [bins,binned] = Bin(x,limits,nBins)

    % To automatically set the limits to [min(x) max(x)]
    bins = Bin(x,nBins)

    % To specify an explicit list of bins (including the upper bound
    % of the last bin)
    [bins,binned] = Bin(x,bins)

    % To discard values out of limits
    bins = Bin(x,...,'trim')

    x              1D variable
    limits         [min_x max_x], where min_x is included and max_x is excluded;
                   these need not be equal to min(x) and max(x)
                   by default, x values out of bounds are counted in the first
                   and last bin, respectively
    nBins          number of bins
    bins           explicit list of bins - including the upper bound of the
                   last bin

  NOTE

    y = Bin(x,[m M],N) uses N bins b(i). Their size is k=(M-m)/N and their
    lower and upper bounds are m+i*k (included) and m+(i+1)*k (excluded),
    respectively. As intended, the extreme bounds are equal to m and M,
    respectively, but keep in mind that while the first bin starts at m,
    the last bin *ends* at M (it actually starts at m+(N-1)*k).

  EXAMPLES

    Bin([2 2.3 4 4.7],[2 5],3);  % returns [1 1 3 3]
    Bin([2 2.3 4 4.7],[2 5],6);  % returns [1 1 5 6]
    Bin([2 2.3 4 4.2 5],[2 5],3);  % returns [1 1 3 3 3]

  SEE

    See also Accumulate.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [bins,binned] = Bin(x,a,b,c)
0002 
0003 %Bin - Assign each input value a bin number between 1 and N.
0004 %
0005 %  Assign each input value a bin number between 1 and N. Also return the
0006 %  corresponding bin values (lower bounds), e.g. assuming that the bins
0007 %  are [0,1] [1,2] [2,3] ... the value 2.3 falls in bin #3 and the bin
0008 %  value (lower bound) is 2.
0009 %
0010 %  USAGE
0011 %
0012 %    % To specify bin limits and # bins
0013 %    [bins,binned] = Bin(x,limits,nBins)
0014 %
0015 %    % To automatically set the limits to [min(x) max(x)]
0016 %    bins = Bin(x,nBins)
0017 %
0018 %    % To specify an explicit list of bins (including the upper bound
0019 %    % of the last bin)
0020 %    [bins,binned] = Bin(x,bins)
0021 %
0022 %    % To discard values out of limits
0023 %    bins = Bin(x,...,'trim')
0024 %
0025 %    x              1D variable
0026 %    limits         [min_x max_x], where min_x is included and max_x is excluded;
0027 %                   these need not be equal to min(x) and max(x)
0028 %                   by default, x values out of bounds are counted in the first
0029 %                   and last bin, respectively
0030 %    nBins          number of bins
0031 %    bins           explicit list of bins - including the upper bound of the
0032 %                   last bin
0033 %
0034 %  NOTE
0035 %
0036 %    y = Bin(x,[m M],N) uses N bins b(i). Their size is k=(M-m)/N and their
0037 %    lower and upper bounds are m+i*k (included) and m+(i+1)*k (excluded),
0038 %    respectively. As intended, the extreme bounds are equal to m and M,
0039 %    respectively, but keep in mind that while the first bin starts at m,
0040 %    the last bin *ends* at M (it actually starts at m+(N-1)*k).
0041 %
0042 %  EXAMPLES
0043 %
0044 %    Bin([2 2.3 4 4.7],[2 5],3);  % returns [1 1 3 3]
0045 %    Bin([2 2.3 4 4.7],[2 5],6);  % returns [1 1 5 6]
0046 %    Bin([2 2.3 4 4.2 5],[2 5],3);  % returns [1 1 3 3 3]
0047 %
0048 %  SEE
0049 %
0050 %    See also Accumulate.
0051 
0052 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0053 %
0054 % This program is free software; you can redistribute it and/or modify
0055 % it under the terms of the GNU General Public License as published by
0056 % the Free Software Foundation; either version 3 of the License, or
0057 % (at your option) any later version.
0058 
0059 if nargin < 2,
0060     error('Incorrect number of parameters (type ''help <a href="matlab:help Bin">Bin</a>'' for details).');
0061 end
0062 
0063 if isempty(x),
0064     bins = [];
0065     binned = [];
0066     return
0067 end
0068 if ~isdvector(x),
0069     error('Data is not a vector (type ''help <a href="matlab:help Bin">Bin</a>'' for details).');
0070 end
0071 
0072 if ~isdvector(a),
0073     error('Incorrect second parameter (type ''help <a href="matlab:help Bin">Bin</a>'' for details).');
0074 end
0075 
0076 if length(a) == 1,
0077     % Second parameter is a scalar, i.e. the number of bins
0078     if ~isiscalar(a,'>0'),
0079         error('Incorrect number of bins (type ''help <a href="matlab:help Bin">Bin</a>'' for details).');
0080     end
0081     nBins = a;
0082     % Automatic limits
0083     limits = [min(x) max(x)];
0084     % Trim?
0085     trim = nargin == 3 && strcmp(lower(b),'trim');
0086 elseif length(a) == 2,
0087     % Second parameter is a pair, i.e. the limits
0088     limits = a;
0089     % Third parameter must be # bins
0090     if ~isiscalar(b,'>0'),
0091         error('Incorrect number of bins (type ''help <a href="matlab:help Bin">Bin</a>'' for details).');
0092     end
0093     nBins = b;
0094     % Trim?
0095     trim = nargin == 4 && strcmp(lower(c),'trim');
0096 else
0097     % Explicit bins
0098     d = a(2) - a(1);
0099 %      if any(abs(diff(a)-d)>eps),
0100 %          error('Incorrect list of bins - bins must be evenly spaced (type ''help <a href="matlab:help Bin">Bin</a>'' for details).');
0101 %      end
0102     % Determine limits and # bins
0103     limits = [a(1) a(end)];
0104     nBins = length(a)-1;
0105     % Trim?
0106     trim = nargin == 3 && strcmp(lower(b),'trim');
0107 end
0108 
0109 if trim,
0110     x(x<limits(1)) = nan;
0111     x(x>=limits(2)) = nan;
0112 else
0113     x(x<limits(1)) = limits(1);
0114     x(x>=limits(2)) = limits(2);
0115 end
0116 
0117 bins = floor((x-limits(1))/(limits(2)-limits(1))*nBins)+1;
0118 
0119 % The following fixes boundary issues but also Matlab rounding errors
0120 if trim,
0121     bins(bins>nBins) = nan;
0122 else
0123     bins(bins>nBins) = nBins;
0124 end
0125 
0126 binSize = (limits(2)-limits(1))/nBins;
0127 i = linspace(limits(1),limits(2),nBins+1);
0128 n = isnan(bins);
0129 binned(~n,1) = i(bins(~n))+binSize/2;
0130 binned(n,1) = NaN;

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005