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}) |
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. |
3428
|
32 ## @end deftypefn |
|
33 |
|
34 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
|
35 ## Created: June 1994 |
1337
|
36 |
2311
|
37 function y = prepad (x, l, c) |
559
|
38 |
1337
|
39 if (nargin == 2) |
559
|
40 c = 0; |
1337
|
41 elseif (nargin < 2 || nargin > 3) |
|
42 usage ("prepad (x, l) or prepad (x, l, c)"); |
559
|
43 endif |
|
44 |
4030
|
45 if (! ismatrix (x)) |
3652
|
46 error ("first argument must be a vector or matrix"); |
4030
|
47 elseif (! isscalar (l)) |
1337
|
48 error ("second argument must be a scaler"); |
559
|
49 endif |
|
50 |
1337
|
51 if (l < 0) |
|
52 error ("second argument must be non-negative"); |
559
|
53 endif |
|
54 |
3652
|
55 [nr, nc] = size (x); |
|
56 if (nr == 1) |
|
57 if (nc >= l) |
|
58 y = x(nc-l+1:nc); |
|
59 else |
|
60 y = [c*ones(1,l-nc), x]; |
|
61 endif |
559
|
62 else |
3652
|
63 if (nr >= l) |
|
64 y = x(nr-l+1:nr,:); |
559
|
65 else |
3652
|
66 y = [c*ones(l-nr,nc); x]; |
559
|
67 endif |
|
68 endif |
|
69 |
|
70 endfunction |