39 lines
1.3 KiB
Matlab
39 lines
1.3 KiB
Matlab
function bins = discretize (x, edges, varargin)
|
|
%DISCRETIZE Which bin each value falls in, for explicit edges (R2015a+).
|
|
%
|
|
% An owuitools compatibility shim; see contains.m for the policy, and
|
|
% histcounts.m for why explicit edges only and for the edge convention,
|
|
% which this shares: the last bin is closed, everything outside the
|
|
% edges is NaN, and so is NaN itself.
|
|
|
|
if (nargin < 2)
|
|
error ('discretize: not enough input arguments');
|
|
end
|
|
if (~isnumeric (edges) || isscalar (edges))
|
|
error (['discretize: this sandbox supports discretize(x, edges) with explicit ' ...
|
|
'edges only - MATLAB''s automatic binning is not reproduced here.']);
|
|
end
|
|
if (~isempty (varargin))
|
|
error ('discretize: this shim takes only (x, edges) - no categorical or option arguments');
|
|
end
|
|
if (~isnumeric (x) && ~islogical (x))
|
|
error ('discretize: x must be numeric');
|
|
end
|
|
edges = edges(:).';
|
|
if (numel (edges) < 2 || any (diff (edges) <= 0))
|
|
error ('discretize: edges must be a vector of at least two increasing values');
|
|
end
|
|
|
|
v = double (x);
|
|
bins = nan (size (v));
|
|
last = numel (edges) - 1;
|
|
for i = 1:last
|
|
if (i < last)
|
|
in = v >= edges(i) & v < edges(i + 1);
|
|
else
|
|
in = v >= edges(i) & v <= edges(i + 1);
|
|
end
|
|
bins(in) = i;
|
|
end
|
|
end
|