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 -*- |
|
21 ## @deftypefn {Function File} {@var{x} =} prepad (@var{x}, @var{l}, @var{c}) |
|
22 ## @deftypefnx {Function File} {@var{x} =} postpad (@var{x}, @var{l}, @var{c}) |
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 ## |
3428
|
28 ## If @code{length (x) > l}, elements from the beginning (end) of |
|
29 ## @var{x} are removed until a vector of length l is obtained. |
|
30 ## @end deftypefn |
|
31 |
|
32 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
|
33 ## Created: June 1994 |
1337
|
34 |
2311
|
35 function y = prepad (x, l, c) |
559
|
36 |
1337
|
37 if (nargin == 2) |
559
|
38 c = 0; |
1337
|
39 elseif (nargin < 2 || nargin > 3) |
|
40 usage ("prepad (x, l) or prepad (x, l, c)"); |
559
|
41 endif |
|
42 |
2716
|
43 if (! is_vector (x)) |
1337
|
44 error ("first argument must be a vector"); |
|
45 elseif (! is_scalar (l)) |
|
46 error ("second argument must be a scaler"); |
559
|
47 endif |
|
48 |
1337
|
49 if (l < 0) |
|
50 error ("second argument must be non-negative"); |
559
|
51 endif |
|
52 |
1337
|
53 lx = length (x); |
559
|
54 |
1337
|
55 if (lx >= l) |
559
|
56 y = x(lx-l+1:lx); |
|
57 else |
1337
|
58 if (rows (x) > 1) |
|
59 tmp = c * ones (l-lx, 1); |
|
60 y = [tmp; x]; |
559
|
61 else |
1337
|
62 tmp = c * ones (1, l-lx); |
|
63 y = [tmp, x]; |
559
|
64 endif |
|
65 endif |
|
66 |
|
67 endfunction |