Mercurial > hg > octave-nkf
annotate scripts/general/prepad.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 9e7ebbaf69ff |
children | f3d52523cde1 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12795
diff
changeset
|
1 ## Copyright (C) 1994-2012 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 |
3428 | 19 ## -*- texinfo -*- |
11539
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Function File} {} prepad (@var{x}, @var{l}) |
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
21 ## @deftypefnx {Function File} {} prepad (@var{x}, @var{l}, @var{c}) |
8545
faccdb98d953
postpad.m, prepad.m: doc fix
John W. Eaton <jwe@octave.org>
parents:
8436
diff
changeset
|
22 ## @deftypefnx {Function File} {} prepad (@var{x}, @var{l}, @var{c}, @var{dim}) |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11539
diff
changeset
|
23 ## Prepend the scalar value @var{c} to the vector @var{x} until it is of length |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11539
diff
changeset
|
24 ## @var{l}. If @var{c} is not given, a value of 0 is used. |
2311 | 25 ## |
11539
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
26 ## If @code{length (@var{x}) > @var{l}}, elements from the beginning of |
3500 | 27 ## @var{x} are removed until a vector of length @var{l} is obtained. |
3652 | 28 ## |
29 ## If @var{x} is a matrix, elements are prepended or removed from each row. | |
4870 | 30 ## |
11539
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
31 ## If the optional argument @var{dim} is given, operate along this |
4870 | 32 ## dimension. |
11539
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
33 ## @seealso{postpad, cat, resize} |
3428 | 34 ## @end deftypefn |
35 | |
36 ## Author: Tony Richardson <arichard@stark.cc.oh.us> | |
37 ## Created: June 1994 | |
1337 | 38 |
4862 | 39 function y = prepad (x, l, c, dim) |
40 | |
41 if (nargin < 2 || nargin > 4) | |
6046 | 42 print_usage (); |
4862 | 43 endif |
44 | |
45 if (nargin < 3 || isempty (c)) | |
46 c = 0; | |
47 else | |
48 if (! isscalar (c)) | |
49 error ("prepad: third argument must be empty or a scalar"); | |
50 endif | |
51 endif | |
559 | 52 |
4862 | 53 nd = ndims (x); |
54 sz = size (x); | |
55 if (nargin < 4) | |
12674
9493880928c8
Use common idiom in m-files for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
56 ## Find the first non-singleton dimension. |
9493880928c8
Use common idiom in m-files for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
57 (dim = find (sz > 1, 1)) || (dim = 1); |
4862 | 58 else |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
59 if (!(isscalar (dim) && dim == fix (dim)) |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
60 || !(1 <= dim && dim <= nd)) |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
61 error ("prepad: DIM must be an integer and a valid dimension"); |
4862 | 62 endif |
559 | 63 endif |
64 | |
5459 | 65 if (! isscalar (l) || l < 0) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
66 error ("prepad: second argument must be a positive scaler"); |
559 | 67 endif |
68 | |
5459 | 69 if (dim > nd) |
70 sz(nd+1:dim) = 1; | |
71 endif | |
72 | |
4862 | 73 d = sz (dim); |
74 | |
75 if (d >= l) | |
12676
2783fa95cab7
Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents:
12674
diff
changeset
|
76 idx = repmat ({':'}, nd, 1); |
7208 | 77 idx{dim} = d-l+1:d; |
78 y = x(idx{:}); | |
559 | 79 else |
4862 | 80 sz (dim) = l - d; |
81 y = cat (dim, c * ones (sz), x); | |
559 | 82 endif |
83 | |
84 endfunction | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
85 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
86 %!error prepad (); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
87 %!error prepad (1); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
88 %!error prepad (1,2,3,4,5); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
89 %!error prepad ([1,2], 2, 2,3); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
90 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
91 %!assert (prepad ([1,2], 4), [0,0,1,2]); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
92 %!assert (prepad ([1;2], 4), [0;0;1;2]); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
93 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
94 %!assert (prepad ([1,2], 4, 2), [2,2,1,2]); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
95 %!assert (prepad ([1;2], 4, 2), [2;2;1;2]); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
96 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
97 %!assert (prepad ([1,2], 2, 2, 1), [2,2;1,2]); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
98 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
99 ## FIXME -- we need tests for multidimensional arrays. |