Mercurial > hg > octave-lyh
changeset 6396:9ec04285a60e
[project @ 2007-03-08 10:08:28 by dbateman]
author | dbateman |
---|---|
date | Thu, 08 Mar 2007 10:08:28 +0000 |
parents | a8dd70bacc1e |
children | 14e5e9d1306f |
files | scripts/ChangeLog scripts/statistics/base/unidrnd.m scripts/statistics/distributions/unidrnd.m |
diffstat | 3 files changed, 40 insertions(+), 80 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2007-03-08 David Bateman <dbateman@free.fr> + + * statistics/base/unidrnd.m: Move to statistics/distributions + replacing slower version based on dicrete_rnd. + 2007-03-07 John W. Eaton <jwe@octave.org> * control/base/rlocus.m: Update for current plotting functions.
deleted file mode 100644 --- a/scripts/statistics/base/unidrnd.m +++ /dev/null @@ -1,61 +0,0 @@ -## Copyright (C) 2005 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} {} unidrnd (@var{mx}); -## @deftypefnx {Function File} {} unidrnd (@var{mx}, @var{v}); -## @deftypefnx {Function File} {} unidrnd (@var{mx}, @var{m}, @var{n}, @dots{}); -## Return random values from discrete uniform distribution, with maximum -## value(s) given by the integer @var{mx}, which may be a scalar or -## multidimensional array. -## -## If @var{mx} is a scalar, the size of the result is specified by -## the vector @var{v}, or by the optional arguments @var{m}, @var{n}, -## @dots{}. Otherwise, the size of the result is the same as the size -## of @var{mx}. -## @end deftypefn - -## Author: jwe - -function retval = unidrnd (n, varargin) - if (nargin == 1) - dims = size (n); - elseif (nargin == 2) - if (rows (varargin{1}) == 1 && columns (varargin{1}) > 1) - dims = varargin{1}; - else - error ("unidrnd: invalid dimension vector"); - endif - elseif (nargin > 2) - for i = 1:nargin-1 - if (! isscalar (varargin{i})) - error ("unidrnd: expecting scalar dimensions"); - endif - endfor - dims = [varargin{:}]; - else - error ("unidrnd (n, ...)"); - endif - if (isscalar (n) - || (length (size (n)) == length (dims) && all (size (n) == dims))) - retval = ceil (rand (dims) .* n); - else - error ("unidrnd: dimension mismatch"); - endif -endfunction
--- a/scripts/statistics/distributions/unidrnd.m +++ b/scripts/statistics/distributions/unidrnd.m @@ -1,4 +1,4 @@ -## Copyright (C) 2007 David Bateman +## Copyright (C) 2005 John W. Eaton ## ## This file is part of Octave. ## @@ -18,28 +18,44 @@ ## 02110-1301, USA. ## -*- texinfo -*- -## @deftypefn {Function File} {} unidrnd (@var{v}, @var{r}, @var{c}) -## @deftypefnx {Function File} {} unidrnd (@var{v}, @var{sz}) -## Generate a row vector containing a random sample of values from -## the univariate distribution which assumes the values in @var{v} with -## eqal probability. If @var{v} is a scalar, it is promoted to @code{1:@var{v}}. +## @deftypefn {Function File} {} unidrnd (@var{mx}); +## @deftypefnx {Function File} {} unidrnd (@var{mx}, @var{v}); +## @deftypefnx {Function File} {} unidrnd (@var{mx}, @var{m}, @var{n}, @dots{}); +## Return random values from discrete uniform distribution, with maximum +## value(s) given by the integer @var{mx}, which may be a scalar or +## multidimensional array. ## -## If @var{r} and @var{c} are given create a matrix with @var{r} rows and -## @var{c} columns. Or if @var{sz} is a vector, create a matrix of size -## @var{sz}. +## If @var{mx} is a scalar, the size of the result is specified by +## the vector @var{v}, or by the optional arguments @var{m}, @var{n}, +## @dots{}. Otherwise, the size of the result is the same as the size +## of @var{mx}. ## @end deftypefn -function rnd = unidrnd (v, varargin) - - if (nargin != 2) - print_usage (); - endif +## Author: jwe - if (isscalar(v)) - v = [1:v].'; +function retval = unidrnd (n, varargin) + if (nargin == 1) + dims = size (n); + elseif (nargin == 2) + if (rows (varargin{1}) == 1 && columns (varargin{1}) > 1) + dims = varargin{1}; + else + error ("unidrnd: invalid dimension vector"); + endif + elseif (nargin > 2) + for i = 1:nargin-1 + if (! isscalar (varargin{i})) + error ("unidrnd: expecting scalar dimensions"); + endif + endfor + dims = [varargin{:}]; else - v = v(:); + error ("unidrnd (n, ...)"); endif - - rnd = discrete_rnd (v, ones(size(v)), varargin{:}); + if (isscalar (n) + || (length (size (n)) == length (dims) && all (size (n) == dims))) + retval = ceil (rand (dims) .* n); + else + error ("unidrnd: dimension mismatch"); + endif endfunction