Mercurial > hg > octave-nkf
annotate scripts/general/prepad.m @ 10845:c0ffe159ba1a
version is now 3.3.52+
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 01 Aug 2010 14:50:51 -0400 |
parents | be55736a0783 |
children | fe3c3dfc07eb |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, |
8920 | 2 ## 2006, 2007, 2008, 2009 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 |
3428 | 20 ## -*- texinfo -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10696
diff
changeset
|
21 ## @deftypefn {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}) |
faccdb98d953
postpad.m, prepad.m: doc fix
John W. Eaton <jwe@octave.org>
parents:
8436
diff
changeset
|
23 ## Prepend (append) the scalar value @var{c} to the vector @var{x} |
3428 | 24 ## until it is of length @var{l}. If the third argument is not |
25 ## supplied, a value of 0 is used. | |
2311 | 26 ## |
3500 | 27 ## If @code{length (@var{x}) > @var{l}}, elements from the beginning (end) of |
28 ## @var{x} are removed until a vector of length @var{l} is obtained. | |
3652 | 29 ## |
30 ## If @var{x} is a matrix, elements are prepended or removed from each row. | |
4870 | 31 ## |
32 ## If the optional @var{dim} argument is given, then operate along this | |
33 ## dimension. | |
8436
342f72c1df1a
In prepad, add reference to postpad.
Francesco Potortì <pot@gnu.org>
parents:
7208
diff
changeset
|
34 ## @seealso{postpad} |
3428 | 35 ## @end deftypefn |
36 | |
37 ## Author: Tony Richardson <arichard@stark.cc.oh.us> | |
38 ## Created: June 1994 | |
1337 | 39 |
4862 | 40 function y = prepad (x, l, c, dim) |
41 | |
42 if (nargin < 2 || nargin > 4) | |
6046 | 43 print_usage (); |
4862 | 44 endif |
45 | |
46 if (nargin < 3 || isempty (c)) | |
47 c = 0; | |
48 else | |
49 if (! isscalar (c)) | |
50 error ("prepad: third argument must be empty or a scalar"); | |
51 endif | |
52 endif | |
559 | 53 |
4862 | 54 nd = ndims (x); |
55 sz = size (x); | |
56 if (nargin < 4) | |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
57 ## Find the first non-singleton dimension |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
58 dim = find (sz > 1, 1); |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
59 if (isempty (dim)) |
4862 | 60 dim = 1; |
61 endif | |
62 else | |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
63 if (!(isscalar (dim) && dim == fix (dim)) || |
10696
e363b09a30c0
prepad.m: Fix typo 'dimm' -> 'dim'.
Ben Abbott <bpabbott@mac.com>
parents:
10690
diff
changeset
|
64 !(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
|
65 error ("prepad: DIM must be an integer and a valid dimension"); |
4862 | 66 endif |
559 | 67 endif |
68 | |
5459 | 69 if (! isscalar (l) || l < 0) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
70 error ("prepad: second argument must be a positive scaler"); |
559 | 71 endif |
72 | |
5459 | 73 if (dim > nd) |
74 sz(nd+1:dim) = 1; | |
75 endif | |
76 | |
4862 | 77 d = sz (dim); |
78 | |
79 if (d >= l) | |
80 idx = cell (); | |
81 for i = 1:nd | |
7208 | 82 idx{i} = 1:sz(i); |
4862 | 83 endfor |
7208 | 84 idx{dim} = d-l+1:d; |
85 y = x(idx{:}); | |
559 | 86 else |
4862 | 87 sz (dim) = l - d; |
88 y = cat (dim, c * ones (sz), x); | |
559 | 89 endif |
90 | |
91 endfunction |