]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/nanmin.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / nanmin.m
1 ## Copyright (C) 2001 Paul Kienzle <pkienzle@users.sf.net>
2 ## Copyright (C) 2003 Alois Schloegl <alois.schloegl@ist.ac.at>
3 ##
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
7 ## version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 ## details.
13 ##
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {[@var{v}, @var{idx}] =} nanmin (@var{X})
19 ## @deftypefnx{Function File} {[@var{v}, @var{idx}] =} nanmin (@var{X}, @var{Y})
20 ## Find the minimal element while ignoring NaN values.
21 ##
22 ## @code{nanmin} is identical to the @code{min} function except that NaN values
23 ## are ignored.  If all values in a column are NaN, the minimum is 
24 ## returned as NaN rather than []. 
25 ##
26 ## @seealso{min, nansum, nanmax, nanmean, nanmedian}
27 ## @end deftypefn
28
29 function [v, idx] = nanmin (X, Y, DIM) 
30   if nargin < 1 || nargin > 3
31     print_usage;
32   elseif nargin == 1 || (nargin == 2 && isempty(Y))
33     nanvals = isnan(X);
34     X(nanvals) = Inf;
35     v = min (X);
36     v(all(nanvals)) = NaN;
37   elseif (nargin == 3 && isempty(Y))
38     nanvals = isnan(X);
39     X(nanvals) = Inf;
40     v = min (X,[],DIM);
41     v(all(nanvals,DIM)) = NaN;
42   else
43     Xnan = isnan(X);
44     Ynan = isnan(Y);
45     X(Xnan) = Inf;
46     Y(Ynan) = Inf;
47     if (nargin == 3)
48       v = min(X,Y,DIM);
49     else
50       v = min(X,Y);
51     endif
52     v(Xnan & Ynan) = NaN;
53   endif
54 endfunction