Mercurial > hg > octave-lyh
annotate scripts/general/common_size.m @ 11190:b1f4bdc276b6
Use CamelCase for 'UniformOutput' option to cellfun.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:37:00 -0700 |
parents | 4cb1522e4d0f |
children | 01ddaedd6ad5 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1999, 2000, 2002, 2004, 2005, 2007 |
2 ## Kurt Hornik | |
9477 | 3 ## Copyright (C) 2009 VZLU Prague |
10034 | 4 ## Copyright (C) 2009 Jaroslav Hajek |
3426 | 5 ## |
3922 | 6 ## This file is part of Octave. |
7 ## | |
8 ## Octave is free software; you can redistribute it and/or modify it | |
9 ## under the terms of the GNU General Public License as published by | |
7016 | 10 ## the Free Software Foundation; either version 3 of the License, or (at |
11 ## your option) any later version. | |
3426 | 12 ## |
3922 | 13 ## Octave is distributed in the hope that it will be useful, but |
2539 | 14 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 16 ## General Public License for more details. |
17 ## | |
2539 | 18 ## You should have received a copy of the GNU General Public License |
7016 | 19 ## along with Octave; see the file COPYING. If not, see |
20 ## <http://www.gnu.org/licenses/>. | |
2539 | 21 |
3369 | 22 ## -*- texinfo -*- |
6547 | 23 ## @deftypefn {Function File} {[@var{err}, @var{y1}, @dots{}] =} common_size (@var{x1}, @dots{}) |
2539 | 24 ## Determine if all input arguments are either scalar or of common |
3369 | 25 ## size. If so, @var{err} is zero, and @var{yi} is a matrix of the |
26 ## common size with all entries equal to @var{xi} if this is a scalar or | |
27 ## @var{xi} otherwise. If the inputs cannot be brought to a common size, | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
28 ## @var{err} is 1, and @var{yi} is @var{xi}. For example: |
3426 | 29 ## |
3369 | 30 ## @example |
31 ## @group | |
32 ## [errorcode, a, b] = common_size ([1 2; 3 4], 5) | |
33 ## @result{} errorcode = 0 | |
34 ## @result{} a = [ 1, 2; 3, 4 ] | |
35 ## @result{} b = [ 5, 5; 5, 5 ] | |
36 ## @end group | |
37 ## @end example | |
3426 | 38 ## |
3369 | 39 ## @noindent |
2539 | 40 ## This is useful for implementing functions where arguments can either |
41 ## be scalars or of common size. | |
3369 | 42 ## @end deftypefn |
2539 | 43 |
5428 | 44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539 | 45 ## Created: 15 October 1994 |
46 ## Adapted-By: jwe | |
9477 | 47 ## Optimized-By: Jaroslav Hajek |
2539 | 48 |
3979 | 49 function [errorcode, varargout] = common_size (varargin) |
3426 | 50 |
2539 | 51 if (nargin < 2) |
52 error ("common_size: only makes sense if nargin >= 2"); | |
53 endif | |
54 | |
9477 | 55 ## Find scalar args. |
11188
4cb1522e4d0f
Use function handle as input to cellfun,
Rik <octave@nomad.inbox5.com>
parents:
10965
diff
changeset
|
56 nscal = cellfun (@numel, varargin) != 1; |
3426 | 57 |
9477 | 58 i = find (nscal, 1); |
59 | |
60 if (isempty (i)) | |
61 errorcode = 0; | |
3979 | 62 varargout = varargin; |
2539 | 63 else |
9477 | 64 match = cellfun (@size_equal, varargin, varargin(i)); |
65 if (any (nscal &! match)) | |
66 errorcode = 1; | |
67 varargout = varargin; | |
68 else | |
69 errorcode = 0; | |
70 if (nargout > 1) | |
71 scal = !nscal; | |
72 varargout = varargin; | |
10034 | 73 if (any (nscal)) |
74 dims = size (varargin{find (nscal, 1)}); | |
11190
b1f4bdc276b6
Use CamelCase for 'UniformOutput' option to cellfun.
Rik <octave@nomad.inbox5.com>
parents:
11188
diff
changeset
|
75 subs = arrayfun (@ones, 1, dims, "UniformOutput", false); |
10965
28ef5a31763d
small rewrite in common_size
Jaroslav Hajek <highegg@gmail.com>
parents:
10821
diff
changeset
|
76 varargout(scal) = cellindexmat (varargin(scal), subs{:}); |
10034 | 77 endif |
2539 | 78 endif |
9477 | 79 endif |
2539 | 80 endif |
81 endfunction |