7017
|
1 ## Copyright (C) 1995, 1996, 1999, 2000, 2002, 2004, 2005, 2007 |
|
2 ## Kurt Hornik |
9477
|
3 ## Copyright (C) 2009 VZLU Prague |
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, |
|
27 ## errorcode 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. |
|
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; |
|
72 varargout(scal) = cellfun (@repmat, varargin(scal), {size(varargin{i})}, ... |
|
73 "UniformOutput", false); |
2539
|
74 endif |
9477
|
75 endif |
2539
|
76 endif |
|
77 endfunction |