1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ## Copyright (C) 2009 VZLU Prague
3 ## Copyright (C) 2009 Jaroslav Hajek
5 ## This file is part of Octave.
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
9 ## the Free Software Foundation; either version 3 of the License, or (at
10 ## your option) any later version.
12 ## Octave is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ## General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with Octave; see the file COPYING. If not, see
19 ## <http://www.gnu.org/licenses/>.
22 ## @deftypefn {Function File} {[@var{err}, @var{y1}, @dots{}] =} common_size (@var{x1}, @dots{})
23 ## Determine if all input arguments are either scalar or of common
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 ## @var{err} is 1, and @var{yi} is @var{xi}. For example:
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 ]
39 ## This is useful for implementing functions where arguments can either
40 ## be scalars or of common size.
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44 ## Created: 15 October 1994
46 ## Optimized-By: Jaroslav Hajek
48 function [errorcode, varargout] = common_size (varargin)
51 error ("common_size: only makes sense if nargin >= 2");
55 nscal = cellfun ("numel", varargin) != 1;
63 match = cellfun ("size_equal", varargin, varargin(i));
64 if (any (nscal &! match))
73 dims = size (varargin{find (nscal, 1)});
74 subs = arrayfun (@ones, 1, dims, "uniformoutput", false);
75 varargout(scal) = cellindexmat (varargin(scal), subs{:});
82 %!error common_size ();
86 %! [err, a, b, c] = common_size (m, 3, 5);
89 %! assert (b, [3,3;3,3]);
90 %! assert (c, [5,5;5,5]);