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 |
2539
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2539
|
19 |
3369
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{err}, @var{y1}, ...] =} common_size (@var{x1}, ...) |
2539
|
22 ## Determine if all input arguments are either scalar or of common |
3369
|
23 ## size. If so, @var{err} is zero, and @var{yi} is a matrix of the |
|
24 ## common size with all entries equal to @var{xi} if this is a scalar or |
|
25 ## @var{xi} otherwise. If the inputs cannot be brought to a common size, |
|
26 ## errorcode is 1, and @var{yi} is @var{xi}. For example, |
3426
|
27 ## |
3369
|
28 ## @example |
|
29 ## @group |
|
30 ## [errorcode, a, b] = common_size ([1 2; 3 4], 5) |
|
31 ## @result{} errorcode = 0 |
|
32 ## @result{} a = [ 1, 2; 3, 4 ] |
|
33 ## @result{} b = [ 5, 5; 5, 5 ] |
|
34 ## @end group |
|
35 ## @end example |
3426
|
36 ## |
3369
|
37 ## @noindent |
2539
|
38 ## This is useful for implementing functions where arguments can either |
|
39 ## be scalars or of common size. |
3369
|
40 ## @end deftypefn |
2539
|
41 |
5428
|
42 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539
|
43 ## Created: 15 October 1994 |
|
44 ## Adapted-By: jwe |
|
45 |
3979
|
46 function [errorcode, varargout] = common_size (varargin) |
3426
|
47 |
2539
|
48 if (nargin < 2) |
|
49 error ("common_size: only makes sense if nargin >= 2"); |
|
50 endif |
|
51 |
4854
|
52 len = 2; |
3979
|
53 for i = 1 : nargin |
4854
|
54 sz = size (varargin{i}); |
|
55 if (length (sz) < len) |
|
56 s(i,:) = [sz, ones(1,len - length(sz))]; |
|
57 else |
|
58 if (length (sz) > len) |
|
59 if (i > 1) |
|
60 s = [s, ones(size(s,1), length(sz) - len)]; |
|
61 endif |
|
62 len = length (sz); |
|
63 endif |
|
64 s(i,:) = sz; |
|
65 endif |
2539
|
66 endfor |
3426
|
67 |
2539
|
68 m = max (s); |
|
69 if (any (any ((s != 1)') & any ((s != ones (nargin, 1) * m)'))) |
|
70 errorcode = 1; |
3979
|
71 varargout = varargin; |
2539
|
72 else |
|
73 errorcode = 0; |
3979
|
74 for i = 1 : nargin |
|
75 varargout{i} = varargin{i}; |
|
76 if (prod (s(i,:)) == 1) |
|
77 varargout{i} *= ones (m); |
2539
|
78 endif |
|
79 endfor |
|
80 endif |
|
81 |
|
82 endfunction |