help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: histc in Octave?


From: Francesco Potorti`
Subject: Re: histc in Octave?
Date: Wed, 17 May 2006 09:00:17 +0200

>"help histc
> HISTC Histogram count.
>    N = HISTC(X,EDGES), for vector X, counts the number of values in X
>    that fall between the elements in the EDGES vector (which must contain
>    monotonically non-decreasing values).  N is a LENGTH(EDGES) vector
>    containing these counts.
>
>    N(k) will count the value X(i) if EDGES(k) <= X(i) < EDGES(k+1).  The
>    last bin will count any values of X that match EDGES(end).  Values
>    outside the values in EDGES are not counted."

The hist function does a very similar function, even if it is not
equal.  If you have
  histc (x, e)
in Matlab, you can obtain very similar results by using
  hist (x, (e(1:len-1)+e(2:len))/2)
in Octave.  However, if you want exactly the same results, you could use
this one, that I adapted quickly (15 minutes, no debugging, no guarantee)
from the Octave 2.9 hist function:

## Copyright (C) 1996, 1997 John W. Eaton
##
## 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 2, 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, write to the Free
## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
## 02110-1301, USA.

## -*- texinfo -*-
## @deftypefn {Function File} {} histc (@var{y}, @var{edges})
## Produce histogram counts or plots.
##
## Plot a histogram of the values in @var{y}, using @var{edges} as the
## edges of the bins: element @var{k} in the output vector counts
## elements of @var{y} satisfying @math{y(k) <= x < y(k+1)}.  The last
## element of the output vector will also count elements of @var{y}
## equal to @var{edges(end)}.
##
## With two output arguments, produce the values @var{nn} and @var{xx} such
## that @code{bar (@var{xx}, @var{nn})} will plot the histogram.
## @end deftypefn
##
## @seealso{hist}

function freq = histc (y, x)

  if (nargin < 1 || nargin > 2)
    usage ("hist (y, x)");
  endif

  arg_is_vector = isvector (y);

  if (rows (y) == 1)
    y = y(:);
  endif

  if (isreal (y))
    max_val = max (y);
    min_val = min (y);
  else
    error ("hist: first argument must be a vector");
  endif

  if (nargin > 1)
    ## nargin is 2
    if (isreal (x))
      if (isvector (x))
        x = x(:);
      endif
      tmp = sort (x);
      if (any (tmp != x))
        warning ("hist: edge values not sorted on input");
        x = tmp;
      endif
    else
      error ("hist: second argument must be a vector");
    endif
  endif

  n = rows (x) - 1;
  freq = zeros (n, columns (y));
  for i = 1:n
    freq(i,:) = sum (y <= x(i) && y < x(i+1));
  endfor
  freq(n,:) += sum (y == x(end));

endfunction

-- 
Francesco Potortì (ricercatore)        Voice: +39 050 315 3058 (op.2111)
ISTI - Area della ricerca CNR          Fax:   +39 050 313 8091
via G. Moruzzi 1, I-56124 Pisa         Email: address@hidden
Web: http://fly.isti.cnr.it/           Key:   fly.isti.cnr.it/public.key


reply via email to

[Prev in Thread] Current Thread [Next in Thread]