søn, 09 03 2008 kl. 14:15 +0100, skrev Søren Hauberg:
> søn, 09 03 2008 kl. 10:50 +0100, skrev Søren Hauberg:
> > Also, does anybody know why 'sumskipnan' isn't implemented as
> > (non-tested code, but you'll get the idea)
> > 
> > function retval = sumskipnan(A, varargin)
> >   A(isnan(A)) = 0;
> >   retval = sum(A, varargin{:});
> > endfunction
> > 
> > ? That would be a lot simpler then the C++ implementation, and would
> > also work for complex matrices.
> I was curious, so I did an implementation of 'sumskipnan' using the
> approach above. The result is somewhat slower, but it supports
> everything that 'sum' supports (complex matrices, Nd matrices). I'm
> attaching the function in case people are interested. Should we replace
> the current implementation with my m-file implementation? I don't use
> the function, so I don't have any strong feelings here.
Like so many times before, the attachment was dropped. Here's an inline
copy:

## Copyright (C) 2008 Soren Hauberg
##
## This program 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 of the License, or
## (at your option) any later version.
##
## This program 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 this program; If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} [EMAIL PROTECTED], @var{n}, @var{ssq}] =}
sumskipnan (@var{x}, @var{dim})
## 
## Adds all non-NaN values.
## 
## Input:
## @table @asis
## @item @var{x}
## Array to sum
## @item @var{dim}
## Dimension to sum.
## Default is 1
## @end table
## 
## Output:
## @table @asis
## @item @var{y}
## Sums.
## @item @var{n}
## Count of valid elements (optional)
## @item @var{ssq}
## Sums of squares (optional)
## @c @item @var{ssqsq}
## @c Sums of squares of squares (optional)
## @end table
## @end deftypefn

function [y, N, ssq] = sumskipnan(A, varargin)
  ## Check input
  if (nargin < 1)
    print_usage();
  endif
  if (!ismatrix(A))
    error("sumskipnan: first input argument must be a matrix");
  endif
  
  ## Perform computations
  nans = isnan(A);
  A(nans) = 0;
  y = sum(A, varargin{:});
  if (nargout >= 2)
    N = sum(double(!nans), varargin{:}); # Convert to double's in case
of 'native'
                                         # argument to 'sum'
  endif
  if (nargout >= 3)
    ssq = sumsq(A, varargin{:});
  endif
endfunction


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to