comparison scripts/general/diff.m @ 4869:b92d59213e63

[project @ 2004-04-21 17:03:02 by jwe]
author jwe
date Wed, 21 Apr 2004 17:03:02 +0000
parents 22bd65326ec1
children 4c8a2e4e0717
comparison
equal deleted inserted replaced
4868:0d7b436d0e87 4869:b92d59213e63
16 ## along with Octave; see the file COPYING. If not, write to the Free 16 ## along with Octave; see the file COPYING. If not, write to the Free
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA 17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
18 ## 02111-1307, USA. 18 ## 02111-1307, USA.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} diff (@var{x}, @var{k}) 21 ## @deftypefn {Function File} {} diff (@var{x}, @var{k}, @var{dim})
22 ## If @var{x} is a vector of length @var{n}, @code{diff (@var{x})} is the 22 ## If @var{x} is a vector of length @var{n}, @code{diff (@var{x})} is the
23 ## vector of first differences 23 ## vector of first differences
24 ## @iftex 24 ## @iftex
25 ## @tex 25 ## @tex
26 ## $x_2 - x_1, \ldots{}, x_n - x_{n-1}$. 26 ## $x_2 - x_1, \ldots{}, x_n - x_{n-1}$.
29 ## @ifinfo 29 ## @ifinfo
30 ## @var{x}(2) - @var{x}(1), @dots{}, @var{x}(n) - @var{x}(n-1). 30 ## @var{x}(2) - @var{x}(1), @dots{}, @var{x}(n) - @var{x}(n-1).
31 ## @end ifinfo 31 ## @end ifinfo
32 ## 32 ##
33 ## If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column 33 ## If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column
34 ## differences. 34 ## differences along the first non-singleton dimension.
35 ## 35 ##
36 ## The second argument is optional. If supplied, @code{diff (@var{x}, 36 ## The second argument is optional. If supplied, @code{diff (@var{x},
37 ## @var{k})}, where @var{k} is a nonnegative integer, returns the 37 ## @var{k})}, where @var{k} is a nonnegative integer, returns the
38 ## @var{k}-th differences. 38 ## @var{k}-th differences. It is possible that @var{k} is larger than
39 ## then first non-singleton dimension of the matrix. In this case,
40 ## @code{diff} continues to take the differences along the next
41 ## non-singleton dimension.
42 ##
43 ## The dimension along which to take the difference can be explicitly
44 ## stated with the optional variable @var{dim}. In this case the
45 ## @var{k}-th order differences are calculated along this dimension.
46 ## In the case where @var{k} exceeds @code{size (@var{x}, @var{dim})}
47 ## then an empty matrix is returned.
39 ## @end deftypefn 48 ## @end deftypefn
40 49
41 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> 50 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
42 ## Created: 2 February 1995 51 ## Created: 2 February 1995
43 ## Adapted-By: jwe 52 ## Adapted-By: jwe
44 53
45 function x = diff (x, k) 54 function x = diff (x, k, dim)
46 55
47 if (nargin == 1) 56 if (nargin < 1 || nargin > 3)
57 usage ("diff (x, k");
58 endif
59
60 if (nargin < 2 || isempty(k))
48 k = 1; 61 k = 1;
49 elseif (nargin == 2) 62 else
50 if (! (isscalar (k) && k == round (k) && k >= 0)) 63 if (! (isscalar (k) && k == round (k) && k >= 0))
51 error ("diff: k must be a nonnegative integer"); 64 error ("diff: k must be a nonnegative integer");
52 elseif (k == 0) 65 elseif (k == 0)
53 return; 66 return;
54 endif 67 endif
68 endif
69
70 nd = ndims (x);
71 sz = size (x);
72 if (nargin != 3)
73 %% Find the first non-singleton dimension
74 dim = 1;
75 while (dim < nd + 1 && sz (dim) == 1)
76 dim = dim + 1;
77 endwhile
78 if (dim > nd)
79 dim = 1;
80 endif
55 else 81 else
56 usage ("diff (x, k"); 82 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 &&
83 dim < (nd + 1))
84 error ("diff: dim must be an integer and valid dimension");
85 endif
57 endif 86 endif
58 87
59 if (isstr (x)) 88 if (isstr (x))
60 error ("diff: symbolic differentiation not (yet) supported"); 89 error ("diff: symbolic differentiation not (yet) supported");
61 elseif (isvector (x)) 90 endif
62 n = length (x); 91
63 if (n <= k) 92
64 x = []; 93 if (nargin == 3)
94 if (sz (dim) <= k)
95 sz(dim) = 0;
96 x = zeros (sz);
65 else 97 else
66 for i = 1 : k 98 n = sz (dim);
67 x = x (2 : (n - i + 1)) - x (1 : (n - i)); 99 idx1 = cell ();
100 for i = 1:nd
101 idx1 {i} = 1:sz(i);
68 endfor 102 endfor
69 endif 103 idx2 = idx1;
70 elseif (ismatrix (x)) 104 for i = 1 : k;
71 n = rows (x); 105 idx1 {dim} = 2 : (n - i + 1);
72 if (n <= k) 106 idx2 {dim} = 1 : (n - i);
73 x = []; 107 x = x (idx1 {:}) - x (idx2 {:});
74 else
75 for i = 1 : k
76 x = x (2 : (n - i + 1), :) - x (1: (n - i), :);
77 endfor 108 endfor
78 endif 109 endif
79 else 110 else
80 x = []; 111 if (sum (sz - 1) < k)
112 x = [];
113 else
114 idx1 = cell ();
115 for i = 1:nd
116 idx1 {i} = 1:sz(i);
117 endfor
118 idx2 = idx1;
119 while (k)
120 n = sz (dim);
121 for i = 1 : min (k, n - 1)
122 idx1 {dim} = 2 : (n - i + 1);
123 idx2 {dim} = 1 : (n - i);
124 x = x (idx1 {:}) - x (idx2 {:});
125 endfor
126 idx1 {dim} = idx2 {dim} = 1;
127 k = k - min (k, n - 1);
128 dim = dim + 1;
129 endwhile
130 endif
81 endif 131 endif
82 132
83 endfunction 133 endfunction