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