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