help-octave
[Top][All Lists]

 From: Heber Farnsworth Subject: Trade-off with looping Date: Fri, 16 Aug 1996 12:51:35 -0700 (PDT)

```Given z which is 1 by n and x which is m by 1 consider the following two
pieces of code which accomplish the same thing:

for i = 1:n
u = z(i) - x;
k = exp(u.^2);
f(i) = mean(k);

and

u = z(ones(m,1),:) - x(:,ones(1,n));
k = exp(u.^2);
f = mean(k);

The second example contains no loops and I would expect it to be faster.
This is indeed the case for smaller values of m (less than 1000).  But
when m is 5000 the first example is about twice as fast.  In both cases n
is 40 (and for my purposes will always be less than 100).

There is apparently a tradeoff between the speed of loops and the
difficulty in working with very large matrices (200,000 elements).  Can
anyone shed some light on where the optimal tradeoff is?  What is the
largest matrix that octave can handle comfortably (or is that system
dependent?).

Heber Farnsworth                               | Department of Finance
Univerity of Washington                        | Box 353200
tele:  (206) 528-0793 home                     | Seattle, WA 98195-3200
tele:  (206) 543-4773 finance     web: http://weber.u.washington.edu/~heberf