changeset 10655:9e4407c19e8d

make median more Matlab compatible (bug #29930)
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 25 May 2010 10:03:58 +0200
parents 984b8b6bee02
children ca836bcdf85e
files scripts/ChangeLog scripts/statistics/base/median.m
diffstat 2 files changed, 9 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,8 @@
+2010-05-25  Jaroslav Hajek  <highegg@gmail.com>
+
+	* statistics/base/median.m: Ensure NaNs where needed. Simplify first
+	non-singleton dimension computation.
+
 2010-05-24  Rik <octave@nomad.inbox5.com>
         * general/private/__isequal__.m, image/image.m, 
         statistics/base/ranks.m: Replace calls to deprecated functions.
--- a/scripts/statistics/base/median.m
+++ b/scripts/statistics/base/median.m
@@ -1,6 +1,6 @@
 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, 2007,
 ##               2008, 2009 John W. Eaton
-## Copyright (C) 2009 VZLU Prague
+## Copyright (C) 2009, 2010 VZLU Prague
 ##
 ## This file is part of Octave.
 ##
@@ -54,7 +54,7 @@
     print_usage ();
   endif
   if (nargin < 2)
-    dim = [find(size (a) != 1, 1), 1](1); # First non-singleton dim.
+    [~, dim] = max (size (a) != 1); # First non-singleton dim.
   endif
 
   if (numel (a) > 0)
@@ -65,6 +65,8 @@
     else
       retval = mean (nth_element (a, k:k+1, dim), dim);
     endif
+    ## Inject NaNs where needed, to be consistent with Matlab.
+    retval(any (isnan (a), dim)) = NaN;
   else
     error ("median: invalid matrix argument");
   endif