changeset 4849:a3440ff5eb14

[project @ 2004-04-06 15:23:56 by jwe]
author jwe
date Tue, 06 Apr 2004 15:25:15 +0000
parents 8122518935e4
children 8cc4818a0de0
files scripts/ChangeLog scripts/statistics/base/var.m src/ChangeLog src/DLD-FUNCTIONS/fftn.cc
diffstat 4 files changed, 48 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Apr 06 15:18:23 2004 +0000
+++ b/scripts/ChangeLog	Tue Apr 06 15:25:15 2004 +0000
@@ -1,3 +1,7 @@
+2004-04-06  David Bateman  <dbateman@free.fr>
+
+	* statistics/base/var.m: Update for NDArrays.  Allow dimension arg.
+
 2004-04-02  David Bateman  <dbateman@free.fr>
 
 	* statistics/base/std.m: Allow optional args for type and dim.
--- a/scripts/statistics/base/var.m	Tue Apr 06 15:18:23 2004 +0000
+++ b/scripts/statistics/base/var.m	Tue Apr 06 15:25:15 2004 +0000
@@ -22,27 +22,54 @@
 ## For vector arguments, return the (real) variance of the values.
 ## For matrix arguments, return a row vector contaning the variance for
 ## each column.
+##
+## The argument @var{opt} determines the type of normalization to use. Valid 
+## values are
+##
+## @table @asis 
+## @item 0:
+##   normalizes with N-1, provides the square root of best unbiased estimator
+##   of the variance [default]
+## @item 1:
+##   normalizes with N, this provides the square root of the second moment
+##   around the mean
+## @end table
+##
+## The third argument @var{dim} determines the dimension along which the 
+## variance is calculated.
 ## @end deftypefn
 
 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
 ## Description: Compute variance
 
-function y = var(x)
+function y = var (x, opt, dim)
 
-  if (nargin != 1)
-    usage ("var (x)");
+  if (nargin < 1 || nargin > 3)
+    usage ("var (x, opt, sim)");
+  endif
+  if (nargin < 3)
+    dim = min (find (size (x) > 1));
+    if (isempty (dim))
+      dim = 1;
+    endif
+  endif
+  if (nargin < 2 || isempty (opt))
+    opt = 0;
   endif
 
-  [nr, nc] = size (x);
-  if (nr == 0 || nc == 0)
+  sz = size (x);
+  if (prod (sz) < 1)
     error ("var: x must not be empty");
-  elseif ((nr == 1) && (nc == 1))
+  elseif (sz(dim) == 1)
     y = 0;
-  elseif ((nr == 1) || (nc == 1))
-    n = length (x);
-    y = sumsq (x - sum (x) / n) / (n - 1);
   else
-    y = sumsq (x - ones (nr, 1) * (sum (x) / nr) ) / (nr - 1);
+    rng = ones (1, length (sz));
+    rng (dim) = sz (dim);
+    if (opt == 0)
+      y = sumsq (x - repmat(mean (x, dim), rng), dim) / (sz(dim) - 1);
+    else
+      y = sumsq (x - repmat(mean (x, dim), rng), dim) / sz(dim);
+    endif
   endif
 
 endfunction
--- a/src/ChangeLog	Tue Apr 06 15:18:23 2004 +0000
+++ b/src/ChangeLog	Tue Apr 06 15:25:15 2004 +0000
@@ -1,3 +1,8 @@
+2004-04-06  David Bateman  <dbateman@free.fr>
+
+	* DLD_FUNCTIONS/fftn.cc: Save result of transpose operation.
+	Check for failure of transpose.
+
 2004-04-02  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* ov-bool.h (octave_bool::bool_array_value): New function.
--- a/src/DLD-FUNCTIONS/fftn.cc	Tue Apr 06 15:18:23 2004 +0000
+++ b/src/DLD-FUNCTIONS/fftn.cc	Tue Apr 06 15:25:15 2004 +0000
@@ -64,9 +64,9 @@
     {
       Matrix val = args(1).matrix_value ();
       if (val.rows () > val.columns ())
-	val.transpose ();
+	val = val.transpose ();
 
-      if (val.columns () != dims.length () || val.rows () != 1)
+      if (error_state || val.columns () != dims.length () || val.rows () != 1)
 	error ("%s: second argument must be a vector of length dim", fcn);
       else
 	{