Mercurial > hg > octave-lyh
comparison scripts/general/diff.m @ 3426:f8dde1807dee
[project @ 2000-01-13 08:40:00 by jwe]
author | jwe |
---|---|
date | Thu, 13 Jan 2000 08:40:53 +0000 |
parents | f37ca3017116 |
children | 434790acb067 |
comparison
equal
deleted
inserted
replaced
3425:8625164a0a39 | 3426:f8dde1807dee |
---|---|
1 ## Copyright (C) 1995, 1996 Kurt Hornik | 1 ## Copyright (C) 1995, 1996 Kurt Hornik |
2 ## | 2 ## |
3 ## This program is free software; you can redistribute it and/or modify | 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 | 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) | 5 ## the Free Software Foundation; either version 2, or (at your option) |
6 ## any later version. | 6 ## any later version. |
7 ## | 7 ## |
8 ## This program is distributed in the hope that it will be useful, but | 8 ## This program is distributed in the hope that it will be useful, but |
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of | 9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 ## General Public License for more details. | 11 ## General Public License for more details. |
12 ## | 12 ## |
13 ## You should have received a copy of the GNU General Public License | 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, | 14 ## along with this file. If not, write to the Free Software Foundation, |
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
16 | 16 |
17 ## -*- texinfo -*- | 17 ## -*- texinfo -*- |
24 ## @end tex | 24 ## @end tex |
25 ## @end iftex | 25 ## @end iftex |
26 ## @ifinfo | 26 ## @ifinfo |
27 ## @var{x}(2) - @var{x}(1), @dots{}, @var{x}(n) - @var{x}(n-1). | 27 ## @var{x}(2) - @var{x}(1), @dots{}, @var{x}(n) - @var{x}(n-1). |
28 ## @end ifinfo | 28 ## @end ifinfo |
29 ## | 29 ## |
30 ## If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column | 30 ## If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column |
31 ## differences. | 31 ## differences. |
32 ## | 32 ## |
33 ## The second argument is optional. If supplied, @code{diff (@var{x}, | 33 ## The second argument is optional. If supplied, @code{diff (@var{x}, |
34 ## @var{k})}, where @var{k} is a nonnegative integer, returns the | 34 ## @var{k})}, where @var{k} is a nonnegative integer, returns the |
35 ## @var{k}-th differences. | 35 ## @var{k}-th differences. |
36 ## @end deftypefn | 36 ## @end deftypefn |
37 | 37 |
38 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> | 38 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
39 ## Created: 2 February 1995 | 39 ## Created: 2 February 1995 |
40 ## Adapted-By: jwe | 40 ## Adapted-By: jwe |
41 | 41 |
42 function x = diff (x, k) | 42 function x = diff (x, k) |
43 | 43 |
44 if (nargin == 1) | 44 if (nargin == 1) |
45 k = 1; | 45 k = 1; |
46 elseif (nargin == 2) | 46 elseif (nargin == 2) |
47 if (! (is_scalar (k) && k == round (k) && k >= 0)) | 47 if (! (is_scalar (k) && k == round (k) && k >= 0)) |
48 error ("diff: k must be a nonnegative integer"); | 48 error ("diff: k must be a nonnegative integer"); |
50 return; | 50 return; |
51 endif | 51 endif |
52 else | 52 else |
53 usage ("diff (x [, k]"); | 53 usage ("diff (x [, k]"); |
54 endif | 54 endif |
55 | 55 |
56 if (isstr (x)) | 56 if (isstr (x)) |
57 error ("diff: symbolic differentiation not (yet) supported"); | 57 error ("diff: symbolic differentiation not (yet) supported"); |
58 elseif (is_vector (x)) | 58 elseif (is_vector (x)) |
59 n = length (x); | 59 n = length (x); |
60 if (n <= k) | 60 if (n <= k) |
61 x = []; | 61 x = []; |
62 else | 62 else |
63 for i = 1 : k | 63 for i = 1 : k |
64 x = x (2 : (n - i + 1)) - x (1 : (n - i)); | 64 x = x (2 : (n - i + 1)) - x (1 : (n - i)); |
65 endfor | 65 endfor |
66 endif | 66 endif |
67 elseif (is_matrix (x)) | 67 elseif (is_matrix (x)) |
68 n = rows (x); | 68 n = rows (x); |
69 if (n <= k) | 69 if (n <= k) |
70 x = []; | 70 x = []; |
71 else | 71 else |
72 for i = 1 : k | 72 for i = 1 : k |
73 x = x (2 : (n - i + 1), :) - x (1: (n - i), :); | 73 x = x (2 : (n - i + 1), :) - x (1: (n - i), :); |
74 endfor | 74 endfor |
75 endif | 75 endif |
76 else | 76 else |
77 x = []; | 77 x = []; |
78 endif | 78 endif |