help-octave
[Top][All Lists]
Advanced

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

Re: histc in Octave?


From: Aakash Dalwani
Subject: Re: histc in Octave?
Date: Thu, 18 May 2006 00:15:37 -0400
User-agent: Thunderbird 1.5.0.2 (Windows/20060308)

Francesco Potorti` wrote:
"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

Thanks for your time and prompt reply. Your gesture is highly appreciated :)

However, hist (x, (e(1:len-1)+e(2:len))/2), gives different results, because even though the hist documentation says that it takes the second vector as the center points, but I the actual behaviour is that for the first bin it counts values from -inf to e(1), so then it doesn't actually count values around the center point.

The function didn't work, it gave me all zeros :) but then you had said no guarantee.


I figured the solution though to serve my purpose:
Based on the documentation of Matlab's histc, the following piece of code does exactly the same thing as histc(X,e)

% X is the vector, e is a vector with the boundaries
freq = zeros(length(e),1);

for i=1:length(X)
 for k=1:length(e)-1
   if(e(k) <= X(i) && X(i) < e(k+1))
     freq(k)=freq(k)+1;
   end
 end

 if ( X(i)== e(end))
   freq(end) = freq(end)+1;
 end
end


John, if you think it is valuable to add this function, let me know

Once again, thanks for your time and reply, I really appreciate your gesture,
Thanks,
Aakash



reply via email to

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