2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2303
|
18 |
3428
|
19 ## -*- texinfo -*- |
3500
|
20 ## @deftypefn {Function File} {} prepad (@var{x}, @var{l}, @var{c}) |
|
21 ## @deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c}) |
4870
|
22 ## @deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c}, @var{dim}) |
2311
|
23 ## |
3428
|
24 ## Prepends (appends) the scalar value @var{c} to the vector @var{x} |
|
25 ## until it is of length @var{l}. If the third argument is not |
|
26 ## supplied, a value of 0 is used. |
2311
|
27 ## |
3500
|
28 ## If @code{length (@var{x}) > @var{l}}, elements from the beginning (end) of |
|
29 ## @var{x} are removed until a vector of length @var{l} is obtained. |
3652
|
30 ## |
|
31 ## If @var{x} is a matrix, elements are prepended or removed from each row. |
4870
|
32 ## |
|
33 ## If the optional @var{dim} argument is given, then operate along this |
|
34 ## dimension. |
3428
|
35 ## @end deftypefn |
|
36 |
|
37 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
|
38 ## Created: June 1994 |
1337
|
39 |
4862
|
40 function y = prepad (x, l, c, dim) |
|
41 |
|
42 if (nargin < 2 || nargin > 4) |
6046
|
43 print_usage (); |
4862
|
44 endif |
|
45 |
|
46 if (nargin < 3 || isempty (c)) |
|
47 c = 0; |
|
48 else |
|
49 if (! isscalar (c)) |
|
50 error ("prepad: third argument must be empty or a scalar"); |
|
51 endif |
|
52 endif |
559
|
53 |
4862
|
54 nd = ndims (x); |
|
55 sz = size (x); |
|
56 if (nargin < 4) |
|
57 %% Find the first non-singleton dimension |
|
58 dim = 1; |
|
59 while (dim < nd + 1 && sz (dim) == 1) |
|
60 dim = dim + 1; |
|
61 endwhile |
|
62 if (dim > nd) |
|
63 dim = 1; |
|
64 endif |
|
65 else |
|
66 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
|
67 dim < (nd + 1)) |
|
68 error ("prepad: dim must be an integer and valid dimension"); |
|
69 endif |
559
|
70 endif |
|
71 |
5459
|
72 if (! isscalar (l) || l < 0) |
4862
|
73 error ("second argument must be a positive scaler"); |
559
|
74 endif |
|
75 |
5459
|
76 if (dim > nd) |
|
77 sz(nd+1:dim) = 1; |
|
78 endif |
|
79 |
4862
|
80 d = sz (dim); |
|
81 |
|
82 if (d >= l) |
|
83 idx = cell (); |
|
84 for i = 1:nd |
|
85 idx {i} = 1:sz(i); |
|
86 endfor |
|
87 idx {dim} = d-l+1:d; |
|
88 y = x (idx {:}); |
559
|
89 else |
4862
|
90 sz (dim) = l - d; |
|
91 y = cat (dim, c * ones (sz), x); |
559
|
92 endif |
|
93 |
|
94 endfunction |