help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Applying each element of a vector to each row of a matrix


From: Francesco Potortì
Subject: Re: Applying each element of a vector to each row of a matrix
Date: Mon, 20 Dec 2010 10:25:58 +0100

>Thanks for that, I did not even think about the bsxfun. For the simple 
>multiplication I still prefer the diag(v)*a way since it is more performant
>
>octave:1> v = rand(1000,1);
>octave:2> a = rand(1000);
>octave:3> tic;diag(v)*a;toc
>Elapsed time is 0.00667596 seconds.
>octave:4> tic;bsxfun(@times, a, v);toc
>Elapsed time is 0.0379739 seconds.

Using logarithms you can have exponentiation too.  This still wins over
bsxfun, even with the overhead of transform - antitransform:

octave> n=5;a=repmat(1:n,n,1)
a =

   1   2   3   4   5
   1   2   3   4   5
   1   2   3   4   5
   1   2   3   4   5
   1   2   3   4   5

octave> bsxfun(@power,a,(1:n)')
ans =

      1      2      3      4      5
      1      4      9     16     25
      1      8     27     64    125
      1     16     81    256    625
      1     32    243   1024   3125

octave> round(exp(diag(1:n)*log(a)))
ans =

      1      2      3      4      5
      1      4      9     16     25
      1      8     27     64    125
      1     16     81    256    625
      1     32    243   1024   3125

octave> n=30;a=repmat(1:n,n,1);
octave> tic;bsxfun(@power,a,(1:n)');toc
Elapsed time is 0.0010259 seconds.
octave> tic;exp(diag(1:n)*log(a));toc
Elapsed time is 0.00050306 seconds.
octave> version
ans = 3.2.4

-- 
Francesco Potortì (ricercatore)        Voice: +39 050 315 3058 (op.2111)
ISTI - Area della ricerca CNR          Fax:   +39 050 315 2040
via G. Moruzzi 1, I-56124 Pisa         Email: address@hidden
(entrance 20, 1st floor, room C71)     Web:   http://fly.isti.cnr.it/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]