2539
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
2539
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
2539
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
2539
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
2539
|
19 |
3369
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} diff (@var{x}, @var{k}) |
|
22 ## If @var{x} is a vector of length @var{n}, @code{diff (@var{x})} is the |
|
23 ## vector of first differences |
|
24 ## @iftex |
|
25 ## @tex |
|
26 ## $x_2 - x_1, \ldots{}, x_n - x_{n-1}$. |
|
27 ## @end tex |
|
28 ## @end iftex |
|
29 ## @ifinfo |
|
30 ## @var{x}(2) - @var{x}(1), @dots{}, @var{x}(n) - @var{x}(n-1). |
|
31 ## @end ifinfo |
3426
|
32 ## |
3369
|
33 ## If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column |
2539
|
34 ## differences. |
3426
|
35 ## |
3369
|
36 ## The second argument is optional. If supplied, @code{diff (@var{x}, |
|
37 ## @var{k})}, where @var{k} is a nonnegative integer, returns the |
|
38 ## @var{k}-th differences. |
|
39 ## @end deftypefn |
2539
|
40 |
|
41 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
42 ## Created: 2 February 1995 |
|
43 ## Adapted-By: jwe |
|
44 |
|
45 function x = diff (x, k) |
3426
|
46 |
2539
|
47 if (nargin == 1) |
|
48 k = 1; |
|
49 elseif (nargin == 2) |
4030
|
50 if (! (isscalar (k) && k == round (k) && k >= 0)) |
2539
|
51 error ("diff: k must be a nonnegative integer"); |
|
52 elseif (k == 0) |
|
53 return; |
|
54 endif |
|
55 else |
3456
|
56 usage ("diff (x, k"); |
2539
|
57 endif |
3426
|
58 |
2539
|
59 if (isstr (x)) |
|
60 error ("diff: symbolic differentiation not (yet) supported"); |
4030
|
61 elseif (isvector (x)) |
2539
|
62 n = length (x); |
|
63 if (n <= k) |
|
64 x = []; |
|
65 else |
|
66 for i = 1 : k |
3426
|
67 x = x (2 : (n - i + 1)) - x (1 : (n - i)); |
2539
|
68 endfor |
|
69 endif |
4030
|
70 elseif (ismatrix (x)) |
2539
|
71 n = rows (x); |
|
72 if (n <= k) |
|
73 x = []; |
|
74 else |
|
75 for i = 1 : k |
3426
|
76 x = x (2 : (n - i + 1), :) - x (1: (n - i), :); |
2539
|
77 endfor |
|
78 endif |
|
79 else |
|
80 x = []; |
|
81 endif |
|
82 |
|
83 endfunction |