Home > matlab > missing > stats > common_size.m

common_size

PURPOSE ^

COMMON_SIZE Checks that all inputs are either scalar or of common size

SYNOPSIS ^

function [errorcode, varargout] = common_size (varargin)

DESCRIPTION ^

 COMMON_SIZE  Checks that all inputs are either scalar or of common size
  [ERR, Y1, ...] = common_size(X1, ...)
  Determine if all input arguments are either scalar or of common
  size.  If so, ERR is zero, and YI is a matrix of the
  common size with all entries equal to XI if this is a scalar or
  XI otherwise.  If the inputs cannot be brought to a common size,
  errorcode is 1, and YI is XI.

  Example:
   [errorcode, a, b] = common_size([1 2; 3 4], 5)
      >> errorcode = 0
      >> a = [ 1, 2; 3, 4 ]
      >> b = [ 5, 5; 5, 5 ]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [errorcode, varargout] = common_size (varargin)
0002 % COMMON_SIZE  Checks that all inputs are either scalar or of common size
0003 %  [ERR, Y1, ...] = common_size(X1, ...)
0004 %  Determine if all input arguments are either scalar or of common
0005 %  size.  If so, ERR is zero, and YI is a matrix of the
0006 %  common size with all entries equal to XI if this is a scalar or
0007 %  XI otherwise.  If the inputs cannot be brought to a common size,
0008 %  errorcode is 1, and YI is XI.
0009 %
0010 %  Example:
0011 %   [errorcode, a, b] = common_size([1 2; 3 4], 5)
0012 %      >> errorcode = 0
0013 %      >> a = [ 1, 2; 3, 4 ]
0014 %      >> b = [ 5, 5; 5, 5 ]
0015 
0016 % Adapted from GNU Octave 3.0.1
0017 % Original file: general/common_size.m
0018 % Original author: KH <Kurt.Hornik@wu-wien.ac.at>
0019 
0020 % Copyright (C) 1995, 1996, 1999, 2000, 2002, 2004, 2005, 2007
0021 %               Kurt Hornik
0022 % Copyright (C) 2008-2009 Dynare Team
0023 %
0024 % This file is part of Dynare.
0025 %
0026 % Dynare is free software: you can redistribute it and/or modify
0027 % it under the terms of the GNU General Public License as published by
0028 % the Free Software Foundation, either version 3 of the License, or
0029 % (at your option) any later version.
0030 %
0031 % Dynare is distributed in the hope that it will be useful,
0032 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0033 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0034 % GNU General Public License for more details.
0035 %
0036 % You should have received a copy of the GNU General Public License
0037 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0038 
0039 if (nargin < 2)
0040     error ('common_size: only makes sense if nargin >= 2');
0041 end
0042 
0043 len = 2;
0044 for i = 1 : nargin
0045     sz =  size (varargin{i});
0046     if (length (sz) < len)
0047         s(i,:) = [sz, ones(1,len - length(sz))];
0048     else
0049         if (length (sz) > len)
0050             if (i > 1)
0051                 s = [s, ones(size(s,1), length(sz) - len)];
0052             end
0053             len = length (sz);
0054         end
0055         s(i,:) = sz;
0056     end
0057 end
0058 
0059 m = max (s);
0060 if (any (any ((s ~= 1)') & any ((s ~= ones (nargin, 1) * m)')))
0061     errorcode = 1;
0062     varargout = varargin;
0063 else
0064     errorcode = 0;
0065     for i = 1 : nargin
0066         varargout{i} = varargin{i};
0067         if (prod (s(i,:)) == 1)
0068             varargout{i} = varargout{i} * ones (m);
0069         end
0070     end
0071 end
0072 
0073 end

Generated on Tue 22-May-2012 02:40:23 by m2html © 2005