function [n, edges] = histcounts (x, edges, varargin) %HISTCOUNTS Bin counts for explicit edges (MATLAB R2014b+). % % An owuitools compatibility shim; see contains.m for the policy. % % Explicit edges only, and that limit is the whole point: MATLAB's % automatic binning picks the number of bins by a documented but % non-trivial rule, and a shim that guessed a different one would hand % back a different histogram without anyone noticing. Called without % edges, this says so instead. % % Edge convention, which is where histcounts and the older `histc` % differ and why this is not a one-line alias: bin i is % [edges(i), edges(i+1)) and the LAST bin is closed, so a value exactly % on the final edge lands in the last bin rather than in one of its own. if (nargin < 1) error ('histcounts: not enough input arguments'); end if (nargin < 2 || ~isnumeric (edges) || isscalar (edges)) error (['histcounts: this sandbox supports histcounts(x, edges) with explicit ' ... 'edges only. MATLAB''s automatic bin selection is not reproduced here - ' ... 'choose the edges, e.g. histcounts(x, linspace(min(x), max(x), 11)), and ' ... 'keep whatever form you want in the code you hand over.']); end if (~isempty (varargin)) error ('histcounts: this shim takes only (x, edges) - no name/value options'); end if (~isnumeric (x) && ~islogical (x)) error ('histcounts: x must be numeric'); end edges = edges(:).'; if (numel (edges) < 2 || any (diff (edges) <= 0)) error ('histcounts: edges must be a vector of at least two increasing values'); end v = double (x(:)); v = v(~isnan (v)); n = zeros (1, numel (edges) - 1); for i = 1:numel (n) if (i < numel (n)) n(i) = sum (v >= edges(i) & v < edges(i + 1)); else n(i) = sum (v >= edges(i) & v <= edges(i + 1)); end end end