2539
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
2539
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
2539
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2539
|
18 |
3369
|
19 ## -*- texinfo -*- |
6547
|
20 ## @deftypefn {Function File} {[@var{err}, @var{y1}, @dots{}] =} common_size (@var{x1}, @dots{}) |
2539
|
21 ## Determine if all input arguments are either scalar or of common |
3369
|
22 ## size. If so, @var{err} is zero, and @var{yi} is a matrix of the |
|
23 ## common size with all entries equal to @var{xi} if this is a scalar or |
|
24 ## @var{xi} otherwise. If the inputs cannot be brought to a common size, |
|
25 ## errorcode is 1, and @var{yi} is @var{xi}. For example, |
3426
|
26 ## |
3369
|
27 ## @example |
|
28 ## @group |
|
29 ## [errorcode, a, b] = common_size ([1 2; 3 4], 5) |
|
30 ## @result{} errorcode = 0 |
|
31 ## @result{} a = [ 1, 2; 3, 4 ] |
|
32 ## @result{} b = [ 5, 5; 5, 5 ] |
|
33 ## @end group |
|
34 ## @end example |
3426
|
35 ## |
3369
|
36 ## @noindent |
2539
|
37 ## This is useful for implementing functions where arguments can either |
|
38 ## be scalars or of common size. |
3369
|
39 ## @end deftypefn |
2539
|
40 |
5428
|
41 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539
|
42 ## Created: 15 October 1994 |
|
43 ## Adapted-By: jwe |
|
44 |
3979
|
45 function [errorcode, varargout] = common_size (varargin) |
3426
|
46 |
2539
|
47 if (nargin < 2) |
|
48 error ("common_size: only makes sense if nargin >= 2"); |
|
49 endif |
|
50 |
4854
|
51 len = 2; |
3979
|
52 for i = 1 : nargin |
4854
|
53 sz = size (varargin{i}); |
|
54 if (length (sz) < len) |
|
55 s(i,:) = [sz, ones(1,len - length(sz))]; |
|
56 else |
|
57 if (length (sz) > len) |
|
58 if (i > 1) |
|
59 s = [s, ones(size(s,1), length(sz) - len)]; |
|
60 endif |
|
61 len = length (sz); |
|
62 endif |
|
63 s(i,:) = sz; |
|
64 endif |
2539
|
65 endfor |
3426
|
66 |
2539
|
67 m = max (s); |
|
68 if (any (any ((s != 1)') & any ((s != ones (nargin, 1) * m)'))) |
|
69 errorcode = 1; |
3979
|
70 varargout = varargin; |
2539
|
71 else |
|
72 errorcode = 0; |
3979
|
73 for i = 1 : nargin |
|
74 varargout{i} = varargin{i}; |
|
75 if (prod (s(i,:)) == 1) |
|
76 varargout{i} *= ones (m); |
2539
|
77 endif |
|
78 endfor |
|
79 endif |
|
80 |
|
81 endfunction |