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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2539
|
19 |
3369
|
20 ## -*- texinfo -*- |
4869
|
21 ## @deftypefn {Function File} {} diff (@var{x}, @var{k}, @var{dim}) |
3369
|
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 |
4869
|
34 ## differences along the first non-singleton dimension. |
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 |
4869
|
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. |
3369
|
48 ## @end deftypefn |
2539
|
49 |
5428
|
50 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539
|
51 ## Created: 2 February 1995 |
|
52 ## Adapted-By: jwe |
|
53 |
4869
|
54 function x = diff (x, k, dim) |
3426
|
55 |
4869
|
56 if (nargin < 1 || nargin > 3) |
|
57 usage ("diff (x, k"); |
|
58 endif |
|
59 |
|
60 if (nargin < 2 || isempty(k)) |
2539
|
61 k = 1; |
4869
|
62 else |
4030
|
63 if (! (isscalar (k) && k == round (k) && k >= 0)) |
2539
|
64 error ("diff: k must be a nonnegative integer"); |
|
65 elseif (k == 0) |
|
66 return; |
|
67 endif |
4869
|
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 |
2539
|
81 else |
4869
|
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 |
2539
|
86 endif |
3426
|
87 |
2539
|
88 if (isstr (x)) |
|
89 error ("diff: symbolic differentiation not (yet) supported"); |
4869
|
90 endif |
|
91 |
|
92 |
|
93 if (nargin == 3) |
|
94 if (sz (dim) <= k) |
|
95 sz(dim) = 0; |
|
96 x = zeros (sz); |
2539
|
97 else |
4869
|
98 n = sz (dim); |
|
99 idx1 = cell (); |
|
100 for i = 1:nd |
|
101 idx1 {i} = 1:sz(i); |
2539
|
102 endfor |
4869
|
103 idx2 = idx1; |
|
104 for i = 1 : k; |
|
105 idx1 {dim} = 2 : (n - i + 1); |
|
106 idx2 {dim} = 1 : (n - i); |
|
107 x = x (idx1 {:}) - x (idx2 {:}); |
2539
|
108 endfor |
|
109 endif |
|
110 else |
4869
|
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 |
2539
|
131 endif |
|
132 |
|
133 endfunction |