view scripts/statistics/histc.m @ 28896:90fea9cc9caa

test: Add expected error message <Invalid call> to BIST tests for nargin. * acosd.m, acot.m, acotd.m, acoth.m, acsc.m, acscd.m, acsch.m, asec.m, asecd.m, asech.m, asind.m, atand.m, cosd.m, cot.m, cotd.m, coth.m, csc.m, cscd.m, csch.m, sec.m, secd.m, sech.m, sind.m, tand.m, cart2pol.m, cart2sph.m, celldisp.m, common_size.m, deal.m, del2.m, fliplr.m, integral2.m, interp1.m, isequal.m, isequaln.m, nextpow2.m, pol2cart.m, quad2d.m, quadl.m, quadv.m, randi.m, rat.m, repelem.m, rescale.m, shiftdim.m, sortrows.m, sph2cart.m, xor.m, convhull.m, delaunay.m, delaunayn.m, griddata.m, griddatan.m, inpolygon.m, voronoi.m, voronoin.m, listdlg.m, msgbox.m, questdlg.m, rmappdata.m, setappdata.m, __gripe_missing_component__.m, get_first_help_sentence.m, type.m, which.m, cmpermute.m, cmunique.m, gray2ind.m, imfinfo.m, imshow.m, imwrite.m, ind2rgb.m, movie.m, rgb2ind.m, importdata.m, bandwidth.m, condeig.m, gls.m, housh.m, linsolve.m, logm.m, lscov.m, normest.m, normest1.m, ols.m, ordeig.m, planerot.m, qzhess.m, rref.m, copyfile.m, delete.m, dos.m, fileparts.m, getfield.m, menu.m, mkdir.m, movefile.m, orderfields.m, publish.m, setfield.m, substruct.m, unix.m, unpack.m, decic.m, ode23.m, ode23s.m, ode45.m, fminsearch.m, lsqnonneg.m, pqpnonneg.m, sqp.m, annotation.m, lighting.m, shading.m, area.m, compass.m, contourc.m, feather.m, fplot.m, hist.m, isocaps.m, isocolors.m, isonormals.m, isosurface.m, ostreamtube.m, pie.m, pie3.m, reducepatch.m, reducevolume.m, rose.m, smooth3.m, stairs.m, stem.m, stem3.m, stream2.m, stream3.m, streamline.m, streamribbon.m, streamtube.m, surfnorm.m, trimesh.m, trisurf.m, colstyle.m, hgload.m, linkprop.m, meshgrid.m, ndgrid.m, padecoef.m, polyfit.m, polyval.m, unmkpp.m, profexport.m, ismember.m, unique.m, movfun.m, movslice.m, periodogram.m, sinc.m, spdiags.m, sprandsym.m, betaincinv.m, ellipke.m, factor.m, gammainc.m, gammaincinv.m, isprime.m, lcm.m, gallery.m, hadamard.m, bounds.m, corrcoef.m, discrete_rnd.m, empirical_rnd.m, histc.m, mode.m, movmad.m, movmax.m, movmean.m, movmedian.m, movmin.m, movprod.m, movstd.m, movsum.m, movvar.m, ranks.m, runlength.m, zscore.m, str2num.m, strchr.m, strsplit.m, strtok.m, untabify.m, assert.m, demo.m, example.m, speed.m, test.m, datenum.m, datevec.m, webread.m, webwrite.m: Add expected error message <Invalid call> to BIST tests for nargin. Remove redundant tests for nargin greater than the number of declared inputs which are now handled by interpreter.
author Rik <rik@octave.org>
date Sun, 11 Oct 2020 21:59:35 -0700
parents 28de41192f3c
children 7854d5752dd2
line wrap: on
line source

########################################################################
##
## Copyright (C) 2009-2020 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {@var{n} =} histc (@var{x}, @var{edges})
## @deftypefnx {} {@var{n} =} histc (@var{x}, @var{edges}, @var{dim})
## @deftypefnx {} {[@var{n}, @var{idx}] =} histc (@dots{})
## Compute histogram counts.
##
## When @var{x} is a vector, the function counts the number of elements of
## @var{x} that fall in the histogram bins defined by @var{edges}.  This
## must be a vector of monotonically increasing values that define the edges
## of the histogram bins.
## @tex
## $n(k)$
## @end tex
## @ifnottex
## @code{@var{n}(k)}
## @end ifnottex
## contains the number of elements in @var{x} for which
## @tex
## $@var{edges}(k) <= @var{x} < @var{edges}(k+1)$.
## @end tex
## @ifnottex
## @code{@var{edges}(k) <= @var{x} < @var{edges}(k+1)}.
## @end ifnottex
## The final element of @var{n} contains the number of elements of @var{x}
## exactly equal to the last element of @var{edges}.
##
## When @var{x} is an @math{N}-dimensional array, the computation is carried
## out along dimension @var{dim}.  If not specified @var{dim} defaults to the
## first non-singleton dimension.
##
## When a second output argument is requested an index matrix is also returned.
## The @var{idx} matrix has the same size as @var{x}.  Each element of
## @var{idx} contains the index of the histogram bin in which the
## corresponding element of @var{x} was counted.
## @seealso{hist}
## @end deftypefn

function [n, idx] = histc (x, edges, dim)

  if (nargin < 2)
    print_usage ();
  endif

  if (! isreal (x))
    error ("histc: X argument must be real-valued, not complex");
  endif

  num_edges = numel (edges);
  if (num_edges == 0)
    warning ("histc: empty EDGES specified\n");
    n = idx = [];
    return;
  endif

  if (! isreal (edges))
    error ("histc: EDGES must be real-valued, not complex");
  else
    ## Make sure 'edges' is sorted
    edges = edges(:);
    if (! issorted (edges) || edges(1) > edges(end))
      warning ("histc: edge values not sorted on input");
      edges = sort (edges);
    endif
  endif

  nd = ndims (x);
  sz = size (x);
  if (nargin < 3)
    ## Find the first non-singleton dimension.
    (dim = find (sz > 1, 1)) || (dim = 1);
  else
    if (!(isscalar (dim) && dim == fix (dim))
        || !(1 <= dim && dim <= nd))
      error ("histc: DIM must be an integer and a valid dimension");
    endif
  endif

  nsz = sz;
  nsz(dim) = num_edges;

  ## the splitting point is 3 bins

  if (num_edges <= 3)

    ## This is the O(M*N) algorithm.

    ## Allocate the histogram
    n = zeros (nsz);

    ## Allocate 'idx'
    if (nargout > 1)
      idx = zeros (sz);
    endif

    ## Prepare indices
    idx1 = cell (1, dim-1);
    for k = 1:length (idx1)
      idx1{k} = 1:sz(k);
    endfor
    idx2 = cell (length (sz) - dim);
    for k = 1:length (idx2)
      idx2{k} = 1:sz(k+dim);
    endfor

    ## Compute the histograms
    for k = 1:num_edges-1
      b = (edges(k) <= x & x < edges(k+1));
      n(idx1{:}, k, idx2{:}) = sum (b, dim);
      if (nargout > 1)
        idx(b) = k;
      endif
    endfor
    b = (x == edges(end));
    n(idx1{:}, num_edges, idx2{:}) = sum (b, dim);
    if (nargout > 1)
      idx(b) = num_edges;
    endif

  else

    ## This is the O(M*log(N) + N) algorithm.

    ## Look-up indices.
    idx = lookup (edges, x);
    ## Zero invalid ones (including NaNs).  x < edges(1) are already zero.
    idx(! (x <= edges(end))) = 0;

    iidx = idx;

    ## In case of matrix input, we adjust the indices.
    if (! isvector (x))
      nl = prod (sz(1:dim-1));
      nn = sz(dim);
      nu = prod (sz(dim+1:end));
      if (nl != 1)
        iidx = (iidx-1) * nl;
        iidx += reshape (kron (ones (1, nn*nu), 1:nl), sz);
      endif
      if (nu != 1)
        ne =length (edges);
        iidx += reshape (kron (nl*ne*(0:nu-1), ones (1, nl*nn)), sz);
      endif
    endif

    ## Select valid elements.
    iidx = iidx(idx != 0);

    ## Call accumarray to sum the indexed elements.
    n = accumarray (iidx(:), 1, nsz);

  endif

endfunction


%!test
%! x = linspace (0, 10, 1001);
%! n = histc (x, 0:10);
%! assert (n, [repmat(100, 1, 10), 1]);

%!test
%! x = repmat (linspace (0, 10, 1001), [2, 1, 3]);
%! n = histc (x, 0:10, 2);
%! assert (n, repmat ([repmat(100, 1, 10), 1], [2, 1, 3]));

## Test input validation
%!error <Invalid call> histc ()
%!error <Invalid call> histc (1)
%!error histc ([1:10 1+i], 2)
%!warning <empty EDGES specified> histc (1:10, []);
%!error histc (1, 1, 3)