Home > wafo > wstats > comnsize.m

comnsize

PURPOSE ^

Check if all input arguments are either scalar or of common size.

SYNOPSIS ^

[errorcode,varargout] = comnsize(varargin)

DESCRIPTION ^

  COMNSIZE Check if all input arguments are either scalar or of common size.
 
  CALL:  [errorcode,y1,y2,...,yN] = comnsize(x1,x2,...,xN);
 
   errorcode = 0 , if all xi are either scalar or of common size.
               1 , if the non-scalar xi do not match in size.
   y1,...,yN = Same as x1,...,xN, except that scalars are transformed to
               a constant matrix with same size as the other inputs.
   x1,...,xN = Input arguments.
 
   COMNSIZE makes sure that the output arguments are of common size.
   This is useful for implementing functions where arguments can either
   be scalars or of common size. 
 
   NOTE:  If the errorcode is 1, then yi = xi.
 
  Examples: 
    A = rand(4,5);B = 2;C = rand(4,5);
    [ec,A1,B1,C1] = comnsize(A,B,C);
    ec2 = comnsize(A,1:2);

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

001 function [errorcode,varargout] = comnsize(varargin)
002 % COMNSIZE Check if all input arguments are either scalar or of common size.
003 %
004 % CALL:  [errorcode,y1,y2,...,yN] = comnsize(x1,x2,...,xN);
005 %
006 %  errorcode = 0 , if all xi are either scalar or of common size.
007 %              1 , if the non-scalar xi do not match in size.
008 %  y1,...,yN = Same as x1,...,xN, except that scalars are transformed to
009 %              a constant matrix with same size as the other inputs.
010 %  x1,...,xN = Input arguments.
011 %
012 %  COMNSIZE makes sure that the output arguments are of common size.
013 %  This is useful for implementing functions where arguments can either
014 %  be scalars or of common size. 
015 %
016 %  NOTE:  If the errorcode is 1, then yi = xi.
017 %
018 % Examples: 
019 %   A = rand(4,5);B = 2;C = rand(4,5);
020 %   [ec,A1,B1,C1] = comnsize(A,B,C);
021 %   ec2 = comnsize(A,1:2);
022 
023 % Tested on: matlab 5.3
024 % History:
025 % revised pab 23.10.2000
026 %  - New name comnsize
027 %  - the input arguments can have a any dimension.
028 %  - Updated documentation
029 %  - Vectorized the calculations a bit more.
030 % Based on common_size.m by
031 %  KH <Kurt.Hornik@ci.tuwien.ac.at> Created: 15 October 1994
032 
033 
034 Np   = nargin;
035 Nout = max(nargout-1,0);
036 if Nout>Np, 
037   error('The number of output arguments is too large.')
038 end
039 
040 Ns =2;
041 sz = zeros(Np,Ns);
042 for ix = 1:Np,
043   tmp = size (varargin{ix});
044   Nt=length(tmp);
045   if Nt>Ns,sz=[sz,ones(Np,Nt-Ns)];Ns=Nt; end % Add singleton dimensions
046   sz(ix,1:Nt)=tmp;
047 end
048 
049 csiz     = max(sz,[],1);        % find common size
050 
051 % Check that non-scalar arguments match in size
052 % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
053 errorcode = (any (any ((sz ~= 1)') & any ((sz ~= csiz(ones(Np, 1),:))')));
054 
055 if Nout==0,return;end
056 
057 isscalar = (prod(sz,2)==1)'; % find  mask to scalars
058 isscalar2 = isscalar(1:Nout); % Mask to those in output 
059 ind  = find(isscalar2);  % indices to scalar arguments
060 if (errorcode|all(isscalar)|isempty(ind)),
061   varargout(1:Nout) = varargin(1:Nout);
062 else  
063   ind0 = find(~isscalar2); % indices to non-scalar arguments
064   if any(ind0),
065     varargout(ind0) = varargin(ind0);
066   end
067   % Make sure the scalar arguments are of size csiz
068   for ix = ind,
069     varargout{ix} = varargin{ix}(ones(csiz));
070   end
071 end
072 return
073 
074 
075 
076 
077 
078 
079 
080 
081 
082 
083 
084 
085 
086 
087

Mathematical Statistics
Centre for Mathematical Sciences
Lund University with Lund Institute of Technology

Comments or corrections to the WAFO group


Generated on Thu 06-Oct-2005 02:21:16 for WAFO by m2html © 2003