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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, 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 |
|
42 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
43 ## Created: 15 October 1994 |
|
44 ## Adapted-By: jwe |
|
45 |
|
46 function [errorcode, ...] = common_size (...) |
3426
|
47 |
2539
|
48 if (nargin < 2) |
|
49 error ("common_size: only makes sense if nargin >= 2"); |
|
50 endif |
|
51 |
|
52 va_start (); |
|
53 for k = 1 : nargin |
|
54 s(k, :) = size (va_arg ()); |
|
55 endfor |
3426
|
56 |
2539
|
57 m = max (s); |
|
58 if (any (any ((s != 1)') & any ((s != ones (nargin, 1) * m)'))) |
|
59 errorcode = 1; |
|
60 va_start (); |
|
61 for k = 1 : nargin |
|
62 vr_val (va_arg ()); |
|
63 endfor |
|
64 else |
|
65 errorcode = 0; |
|
66 va_start (); |
|
67 for k = 1 : nargin |
|
68 if (prod (s(k, :)) == 1) |
3426
|
69 vr_val (va_arg () * ones (m)); |
2539
|
70 else |
3426
|
71 vr_val (va_arg ()); |
2539
|
72 endif |
|
73 endfor |
|
74 endif |
|
75 |
|
76 endfunction |