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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
2303
|
19 |
3428
|
20 ## -*- texinfo -*- |
3500
|
21 ## @deftypefn {Function File} {} prepad (@var{x}, @var{l}, @var{c}) |
|
22 ## @deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c}) |
4870
|
23 ## @deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c}, @var{dim}) |
2311
|
24 ## |
3428
|
25 ## Prepends (appends) the scalar value @var{c} to the vector @var{x} |
|
26 ## until it is of length @var{l}. If the third argument is not |
|
27 ## supplied, a value of 0 is used. |
2311
|
28 ## |
3500
|
29 ## If @code{length (@var{x}) > @var{l}}, elements from the beginning (end) of |
|
30 ## @var{x} are removed until a vector of length @var{l} is obtained. |
3652
|
31 ## |
|
32 ## If @var{x} is a matrix, elements are prepended or removed from each row. |
4870
|
33 ## |
|
34 ## If the optional @var{dim} argument is given, then operate along this |
|
35 ## dimension. |
3428
|
36 ## @end deftypefn |
|
37 |
|
38 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
|
39 ## Created: June 1994 |
1337
|
40 |
4862
|
41 function y = prepad (x, l, c, dim) |
|
42 |
|
43 if (nargin < 2 || nargin > 4) |
|
44 usage ("prepad (x, l, [c, [dim]])"); |
|
45 endif |
|
46 |
|
47 if (nargin < 3 || isempty (c)) |
|
48 c = 0; |
|
49 else |
|
50 if (! isscalar (c)) |
|
51 error ("prepad: third argument must be empty or a scalar"); |
|
52 endif |
|
53 endif |
559
|
54 |
4862
|
55 nd = ndims (x); |
|
56 sz = size (x); |
|
57 if (nargin < 4) |
|
58 %% Find the first non-singleton dimension |
|
59 dim = 1; |
|
60 while (dim < nd + 1 && sz (dim) == 1) |
|
61 dim = dim + 1; |
|
62 endwhile |
|
63 if (dim > nd) |
|
64 dim = 1; |
|
65 endif |
|
66 else |
|
67 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
|
68 dim < (nd + 1)) |
|
69 error ("prepad: dim must be an integer and valid dimension"); |
|
70 endif |
559
|
71 endif |
|
72 |
4030
|
73 if (! ismatrix (x)) |
3652
|
74 error ("first argument must be a vector or matrix"); |
4862
|
75 elseif (! isscalar (l) || l < 0) |
|
76 error ("second argument must be a positive scaler"); |
559
|
77 endif |
|
78 |
4862
|
79 d = sz (dim); |
|
80 |
|
81 if (d >= l) |
|
82 idx = cell (); |
|
83 for i = 1:nd |
|
84 idx {i} = 1:sz(i); |
|
85 endfor |
|
86 idx {dim} = d-l+1:d; |
|
87 y = x (idx {:}); |
559
|
88 else |
4862
|
89 sz (dim) = l - d; |
|
90 y = cat (dim, c * ones (sz), x); |
559
|
91 endif |
|
92 |
|
93 endfunction |