7017
|
1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, |
|
2 ## 2006, 2007 John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
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) |
6046
|
44 print_usage (); |
4862
|
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 |
5459
|
73 if (! isscalar (l) || l < 0) |
4862
|
74 error ("second argument must be a positive scaler"); |
559
|
75 endif |
|
76 |
5459
|
77 if (dim > nd) |
|
78 sz(nd+1:dim) = 1; |
|
79 endif |
|
80 |
4862
|
81 d = sz (dim); |
|
82 |
|
83 if (d >= l) |
|
84 idx = cell (); |
|
85 for i = 1:nd |
7208
|
86 idx{i} = 1:sz(i); |
4862
|
87 endfor |
7208
|
88 idx{dim} = d-l+1:d; |
|
89 y = x(idx{:}); |
559
|
90 else |
4862
|
91 sz (dim) = l - d; |
|
92 y = cat (dim, c * ones (sz), x); |
559
|
93 endif |
|
94 |
|
95 endfunction |