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