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