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 |
3458
|
19 ## -*- texinfo -*- |
3500
|
20 ## @deftypefn {Function File} {} postpad (@var{x}, @var{l}, @var{c}) |
3428
|
21 ## See prepad. |
3458
|
22 ## @end deftypefn |
3428
|
23 |
|
24 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
|
25 ## Created: June 1994 |
904
|
26 |
4862
|
27 function y = postpad (x, l, c, dim) |
|
28 |
|
29 if (nargin < 2 || nargin > 4) |
6046
|
30 print_usage (); |
4862
|
31 endif |
|
32 |
|
33 if (nargin < 3 || isempty (c)) |
|
34 c = 0; |
|
35 else |
|
36 if (! isscalar (c)) |
|
37 error ("postpad: third argument must be empty or a scalar"); |
|
38 endif |
|
39 endif |
561
|
40 |
4862
|
41 nd = ndims (x); |
|
42 sz = size (x); |
|
43 if (nargin < 4) |
|
44 %% Find the first non-singleton dimension |
|
45 dim = 1; |
|
46 while (dim < nd + 1 && sz (dim) == 1) |
|
47 dim = dim + 1; |
|
48 endwhile |
|
49 if (dim > nd) |
|
50 dim = 1; |
|
51 endif |
|
52 else |
|
53 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
|
54 dim < (nd + 1)) |
|
55 error ("postpad: dim must be an integer and valid dimension"); |
|
56 endif |
561
|
57 endif |
|
58 |
5459
|
59 if (! isscalar (l) || l < 0) |
4862
|
60 error ("second argument must be a positive scaler"); |
561
|
61 endif |
|
62 |
5459
|
63 if (dim > nd) |
|
64 sz(nd+1:dim) = 1; |
|
65 endif |
|
66 |
4862
|
67 d = sz (dim); |
5459
|
68 |
4862
|
69 if (d >= l) |
|
70 idx = cell (); |
|
71 for i = 1:nd |
|
72 idx {i} = 1:sz(i); |
|
73 endfor |
|
74 idx {dim} = 1:l; |
|
75 y = x (idx {:}); |
561
|
76 else |
4862
|
77 sz (dim) = l - d; |
|
78 y = cat (dim, x, c * ones (sz)); |
561
|
79 endif |
|
80 |
|
81 endfunction |