Mercurial > octave
annotate scripts/general/postpad.m @ 20852:516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 12 Dec 2015 07:31:00 -0800 |
parents | 7503499a252b |
children | 30eefd9ddb48 |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19654
diff
changeset
|
1 ## Copyright (C) 1994-2015 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 -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
20 ## @deftypefn {} {} postpad (@var{x}, @var{l}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
21 ## @deftypefnx {} {} postpad (@var{x}, @var{l}, @var{c}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
22 ## @deftypefnx {} {} postpad (@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 ## Append 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. |
11539
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
25 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
26 ## If @code{length (@var{x}) > @var{l}}, elements from the end of @var{x} are |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## removed until a vector of length @var{l} is obtained. |
11539
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
28 ## |
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
29 ## If @var{x} is a matrix, elements are appended or removed from each row. |
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
30 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
31 ## If the optional argument @var{dim} is given, operate along this dimension. |
18513
d28c4c4547ef
Allow postpad and prepad to expand singleton dimensions (bug #41633)
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
32 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## If @var{dim} is larger than the dimensions of @var{x}, the result will have |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## @var{dim} dimensions. |
11539
6bac61388876
Add undocumented postpad function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
35 ## @seealso{prepad, cat, resize} |
3458 | 36 ## @end deftypefn |
3428 | 37 |
38 ## Author: Tony Richardson <arichard@stark.cc.oh.us> | |
39 ## Created: June 1994 | |
904 | 40 |
4862 | 41 function y = postpad (x, l, c, dim) |
42 | |
43 if (nargin < 2 || nargin > 4) | |
6046 | 44 print_usage (); |
4862 | 45 endif |
46 | |
47 if (nargin < 3 || isempty (c)) | |
48 c = 0; | |
49 else | |
50 if (! isscalar (c)) | |
51 error ("postpad: third argument must be empty or a scalar"); | |
52 endif | |
53 endif | |
561 | 54 |
4862 | 55 nd = ndims (x); |
56 sz = size (x); | |
57 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
|
58 ## 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
|
59 (dim = find (sz > 1, 1)) || (dim = 1); |
4862 | 60 else |
18513
d28c4c4547ef
Allow postpad and prepad to expand singleton dimensions (bug #41633)
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
61 if (!(isscalar (dim) && dim == fix (dim) && dim >= 1)) |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
62 error ("postpad: DIM must be an integer and a valid dimension"); |
4862 | 63 endif |
561 | 64 endif |
65 | |
5459 | 66 if (! isscalar (l) || l < 0) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
67 error ("postpad: second argument must be a positive scaler"); |
561 | 68 endif |
69 | |
5459 | 70 if (dim > nd) |
71 sz(nd+1:dim) = 1; | |
72 endif | |
73 | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
74 d = sz(dim); |
5459 | 75 |
4862 | 76 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
|
77 idx = repmat ({':'}, nd, 1); |
7208 | 78 idx{dim} = 1:l; |
79 y = x(idx{:}); | |
561 | 80 else |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
81 sz(dim) = l - d; |
16304
c8e1b0213e34
pkg.m: Fix str-to-num warnings (bug #37785)
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
82 y = cat (dim, x, c(ones (sz))); |
561 | 83 endif |
84 | |
85 endfunction | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
86 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
87 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %!assert (postpad ([1,2], 4), [1,2,0,0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %!assert (postpad ([1;2], 4), [1;2;0;0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 %!assert (postpad ([1,2], 4, 2), [1,2,2,2]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 %!assert (postpad ([1;2], 4, 2), [1;2;2;2]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 %!assert (postpad ([1,2], 2, 2, 1), [1,2;2,2]) |
18513
d28c4c4547ef
Allow postpad and prepad to expand singleton dimensions (bug #41633)
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
93 %!assert (postpad ([1;2], 2, 2, 3), reshape ([1;2;2;2], 2, 1, 2)) |
d28c4c4547ef
Allow postpad and prepad to expand singleton dimensions (bug #41633)
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
94 %!assert (postpad ([1,2], 2, 2, 3), reshape ([1,2,2,2], 1, 2, 2)) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
95 |
19654
107ec5195a47
prepad.m: Fix str-to-num warnings (bug #44162)
Mike Miller <mtmiller@ieee.org>
parents:
18513
diff
changeset
|
96 %! ## Test with string concatenation (bug #44162) |
107ec5195a47
prepad.m: Fix str-to-num warnings (bug #44162)
Mike Miller <mtmiller@ieee.org>
parents:
18513
diff
changeset
|
97 %!assert (postpad ("Octave", 16, "x"), "Octavexxxxxxxxxx") |
107ec5195a47
prepad.m: Fix str-to-num warnings (bug #44162)
Mike Miller <mtmiller@ieee.org>
parents:
18513
diff
changeset
|
98 %!assert (postpad ("Octave", 4), "Octa") |
107ec5195a47
prepad.m: Fix str-to-num warnings (bug #44162)
Mike Miller <mtmiller@ieee.org>
parents:
18513
diff
changeset
|
99 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
100 %!error postpad () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
101 %!error postpad (1) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 %!error postpad (1,2,3,4,5) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12676
diff
changeset
|
103 |