comparison scripts/general/trapz.m @ 10690:35adf2a71f3f

Use common code block to find first non-singleton dimension.
author Rik <octave@nomad.inbox5.com>
date Tue, 08 Jun 2010 22:09:25 -0700
parents 95c3e38098bf
children be55736a0783
comparison
equal deleted inserted replaced
10689:6622772a0add 10690:35adf2a71f3f
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{z} =} trapz (@var{y}) 20 ## @deftypefn {Function File} {@var{z} =} trapz (@var{y})
21 ## @deftypefnx {Function File} {@var{z} =} trapz (@var{x}, @var{y}) 21 ## @deftypefnx {Function File} {@var{z} =} trapz (@var{x}, @var{y})
22 ## @deftypefnx {Function File} {@var{z} =} trapz (@dots{}, @var{dim}) 22 ## @deftypefnx {Function File} {@var{z} =} trapz (@dots{}, @var{dim})
23 ## 23 ##
24 ## Numerical integration using trapezoidal method. @code{trapz 24 ## Numerical integration using trapezoidal method. @code{trapz
25 ## (@var{y})} computes the integral of the @var{y} along the first 25 ## (@var{y})} computes the integral of the @var{y} along the first
35 ## also: June 2000 - Paul Kienzle (fixes,suggestions) 35 ## also: June 2000 - Paul Kienzle (fixes,suggestions)
36 ## 2006-05-12 David Bateman - Modified for NDArrays 36 ## 2006-05-12 David Bateman - Modified for NDArrays
37 37
38 function z = trapz (x, y, dim) 38 function z = trapz (x, y, dim)
39 39
40
41 if (nargin < 1) || (nargin > 3) 40 if (nargin < 1) || (nargin > 3)
42 print_usage (); 41 print_usage ();
43 endif 42 endif
44 43
45 nd = ndims (x); 44 nd = ndims (x);
59 have_x = true; 58 have_x = true;
60 endif 59 endif
61 endif 60 endif
62 61
63 if (! have_dim) 62 if (! have_dim)
64 ## Find the first singleton dimension. 63 ## Find the first non-singleton dimension.
65 dim = 0; 64 dim = find (sz > 1, 1);
66 while (dim < nd && sz(dim+1) == 1) 65 if (isempty (dim))
67 dim++;
68 endwhile
69 dim++;
70 if (dim > nd)
71 dim = 1; 66 dim = 1;
72 endif 67 endif
73 else 68 else
74 dim = floor (dim); 69 dim = floor (dim);
75 if (dim < 1 || dim > nd) 70 if (dim < 1 || dim > nd)
76 error ("trapz: invalid dimension along which to sort"); 71 error ("trapz: invalid dimension DIM along which to sort");
77 endif 72 endif
78 endif 73 endif
79 74
80 n = sz(dim); 75 n = sz(dim);
81 idx1 = cell (); 76 idx1 = cell ();