From octave-sources-request at bevo dot che dot wisc dot edu Thu Oct 5 16:01:22 2000 Subject: outer.m (again) From: Paul Kienzle To: octave-sources at bevo dot che dot wisc dot edu Cc: mathyant at student dot ethz dot ch Date: Thu, 05 Oct 2000 11:56:06 +0100 A further x3 speedup for outer(x,y) by using * rather than .* ## Copyright (C) 2000 Antoine Mathys ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. ## -*- texinfo -*- ## at deftypefn {Function File} {@var{a} =} outer (@var{x}, @var{y}, [@var{f}]) ## Given two vectors of length at var{m} and @var{n}, return a ## at var{m}-by-@var{n} matrix with entries @var{f (x(i), y(j))}. ## ## If at var{f} is omitted, multiplication is the default. ## ## at deftypefnx {Function File} {@var{a} =} outer (@var{x}, @var{y}, @var{f}, 'matrix') ## Pass matrices to function at var{f} instead of individual elements. ## at end deftypefn ## Author: Antoine Mathys ## Created: 2 October 2000 ## Adapted-By: Antoine Mathys function retval = outer (x, y, f, matrix) if (nargin < 2 || nargin > 4) usage ("outer (x, y [, f [, 'matrix']])"); endif if (!is_vector(x) || !is_vector(y)) error ("outer: both x and y must be vectors"); endif m = length (x); n = length (y); if nargin == 2 retval = x(:) * y(:).'; elseif nargin == 4 x = x (:); x = x (:, ones (1, n)); y = y (:).'; y = y (ones (1, m), :); retval = feval (f, x, y); else retval = zeros (m, n); for i=1:m for j=1:n retval (i, j) = feval (f, x(i), y(j)); endfor endfor endif endfunction ----------------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.che.wisc.edu/octave/octave.html How to fund new projects: http://www.che.wisc.edu/octave/funding.html Subscription information: http://www.che.wisc.edu/octave/archive.html -----------------------------------------------------------------------