1337
|
1 function y = postpad (x, l, c) |
904
|
2 |
1337
|
3 # postpad (x, l) |
|
4 # |
904
|
5 # Appends zeros to the vector x until it is of length l. |
1337
|
6 # postpad (x, l, c) appends the constant c instead of zero. |
561
|
7 # |
1337
|
8 # If length (x) > l, elements from the end of x are removed |
904
|
9 # until a vector of length l is obtained. |
561
|
10 |
|
11 # Author: |
|
12 # Tony Richardson |
|
13 # amr@mpl.ucsd.edu |
|
14 # June 1994 |
|
15 |
1337
|
16 if (nargin == 2) |
561
|
17 c = 0; |
1337
|
18 elseif (nargin < 2 || nargin > 3) |
|
19 usage ("postpad (x, l) or postpad (x, l, c)"); |
561
|
20 endif |
|
21 |
1337
|
22 if (is_matrix (x)) |
|
23 error ("first argument must be a vector"); |
|
24 elseif (! is_scalar (l)) |
|
25 error ("second argument must be a scaler"); |
561
|
26 endif |
|
27 |
1337
|
28 if (l < 0) |
|
29 error ("second argument must be non-negative"); |
561
|
30 endif |
|
31 |
1337
|
32 lx = length (x); |
561
|
33 |
1337
|
34 if (lx >= l) |
561
|
35 y = x(1:l); |
|
36 else |
1337
|
37 if (rows (x) > 1) |
|
38 tmp = c * ones (l-lx, 1); |
|
39 y = [x; tmp]; |
561
|
40 else |
1337
|
41 tmp = c * ones (1, l-lx); |
|
42 y = [x, tmp]; |
561
|
43 endif |
|
44 endif |
|
45 |
|
46 endfunction |