Mercurial > hg > octave-lyh
annotate scripts/general/postpad.m @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | fe3c3dfc07eb |
children | fd0a3ac60b0e |
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 |
3458 | 20 ## -*- texinfo -*- |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
21 ## @deftypefn {Function File} {} postpad (@var{x}, @var{l}, @var{c}) |
8545
faccdb98d953
postpad.m, prepad.m: doc fix
John W. Eaton <jwe@octave.org>
parents:
8433
diff
changeset
|
22 ## @deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c}, @var{dim}) |
8433
3292bd1bbaa4
Cross reference between postpad and resize
Francesco Potortì <pot@gnu.org>
parents:
7208
diff
changeset
|
23 ## @seealso{prepad, resize} |
3458 | 24 ## @end deftypefn |
3428 | 25 |
26 ## Author: Tony Richardson <arichard@stark.cc.oh.us> | |
27 ## Created: June 1994 | |
904 | 28 |
4862 | 29 function y = postpad (x, l, c, dim) |
30 | |
31 if (nargin < 2 || nargin > 4) | |
6046 | 32 print_usage (); |
4862 | 33 endif |
34 | |
35 if (nargin < 3 || isempty (c)) | |
36 c = 0; | |
37 else | |
38 if (! isscalar (c)) | |
39 error ("postpad: third argument must be empty or a scalar"); | |
40 endif | |
41 endif | |
561 | 42 |
4862 | 43 nd = ndims (x); |
44 sz = size (x); | |
45 if (nargin < 4) | |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
46 ## 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
|
47 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
|
48 if (isempty (dim)) |
4862 | 49 dim = 1; |
50 endif | |
51 else | |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10690
diff
changeset
|
52 if (!(isscalar (dim) && dim == fix (dim)) |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10690
diff
changeset
|
53 || !(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
|
54 error ("postpad: DIM must be an integer and a valid dimension"); |
4862 | 55 endif |
561 | 56 endif |
57 | |
5459 | 58 if (! isscalar (l) || l < 0) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
59 error ("postpad: second argument must be a positive scaler"); |
561 | 60 endif |
61 | |
5459 | 62 if (dim > nd) |
63 sz(nd+1:dim) = 1; | |
64 endif | |
65 | |
4862 | 66 d = sz (dim); |
5459 | 67 |
4862 | 68 if (d >= l) |
69 idx = cell (); | |
70 for i = 1:nd | |
7208 | 71 idx{i} = 1:sz(i); |
4862 | 72 endfor |
7208 | 73 idx{dim} = 1:l; |
74 y = x(idx{:}); | |
561 | 75 else |
4862 | 76 sz (dim) = l - d; |
77 y = cat (dim, x, c * ones (sz)); | |
561 | 78 endif |
79 | |
80 endfunction |