help-octave
[Top][All Lists]

## Re: A Simple Matrix Construction Question

 From: John W. Eaton Subject: Re: A Simple Matrix Construction Question Date: Thu, 23 Jan 2003 13:57:15 -0600

```On 23-Jan-2003, Carlo de Falco <address@hidden> wrote:

|
| GiovedÃ¬, 23 Gen 2003, alle 20:34 Europe/Rome, David Pruitt ha scritto:
|
| > This is known as "Tony's trick" from the Matlab support website.  My
| > question is: why does this work?
| >
|
| M = Y(ones(5,1),:)
|
| means:
|
| M = [ Y(1,1:5)
|       Y(1,1:5)
|       Y(1,1:5)
|       Y(1,1:5)
|       Y(1,1:5)
| ];
|
| in other words, replicate Y five times with first index always 1 and
| second index spanning 1:5
| neat!

You might be surprised to find that the multiplication solution can be
faster than either this trick or kron, particularly if your copy of
Octave is linked with ATLAS.  The reason is that indexing is not all
that fast in Octave, and kron is not optimized in the same way that
the matrix multiply routine from the BLAS is.  For example, on my
system:

octave:1> y = rand (1, 1000);
octave:2> x = ones (1000, 1);
octave:3> t = cputime (); y(x,:); cputime () - t
ans = 0.11000
octave:4> t = cputime (); kron (x, y); cputime () - t
ans = 0.070000
octave:5> t = cputime (); x*y; cputime () - t
ans = 0.030000

jwe

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```

reply via email to