home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / general / common_size.m < prev    next >
Encoding:
Text File  |  1999-11-21  |  2.2 KB  |  74 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## -*- texinfo -*-
  18. ## @deftypefn {Function File} {[@var{err}, @var{y1}, ...] =} common_size (@var{x1}, ...)
  19. ## Determine if all input arguments are either scalar or of common
  20. ## size.  If so, @var{err} is zero, and @var{yi} is a matrix of the
  21. ## common size with all entries equal to @var{xi} if this is a scalar or
  22. ## @var{xi} otherwise.  If the inputs cannot be brought to a common size,
  23. ## errorcode is 1, and @var{yi} is @var{xi}.  For example,
  24. ## 
  25. ## @example
  26. ## @group
  27. ## [errorcode, a, b] = common_size ([1 2; 3 4], 5)
  28. ##      @result{} errorcode = 0
  29. ##      @result{} a = [ 1, 2; 3, 4 ]
  30. ##      @result{} b = [ 5, 5; 5, 5 ]
  31. ## @end group
  32. ## @end example
  33. ## 
  34. ## @noindent
  35. ## This is useful for implementing functions where arguments can either
  36. ## be scalars or of common size.
  37. ## @end deftypefn
  38.  
  39. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  40. ## Created: 15 October 1994
  41. ## Adapted-By: jwe
  42.  
  43. function [errorcode, ...] = common_size (...)
  44.   
  45.   if (nargin < 2)
  46.     error ("common_size: only makes sense if nargin >= 2");
  47.   endif
  48.  
  49.   va_start ();
  50.   for k = 1 : nargin
  51.     s(k, :) = size (va_arg ());
  52.   endfor
  53.   
  54.   m = max (s);
  55.   if (any (any ((s != 1)') & any ((s != ones (nargin, 1) * m)')))
  56.     errorcode = 1;
  57.     va_start ();
  58.     for k = 1 : nargin
  59.       vr_val (va_arg ());
  60.     endfor
  61.   else
  62.     errorcode = 0;
  63.     va_start ();
  64.     for k = 1 : nargin
  65.       if (prod (s(k, :)) == 1)
  66.     vr_val (va_arg () * ones (m));
  67.       else
  68.     vr_val (va_arg ());
  69.       endif
  70.     endfor
  71.   endif
  72.  
  73. endfunction
  74.