help-octave
[Top][All Lists]

## Re: simple vector to matrix transformation

 From: Daniel Heiserer Subject: Re: simple vector to matrix transformation Date: Tue, 22 Jun 1999 15:35:43 +0200

```address@hidden wrote:
>
> I just needed to add the same vector to every column of a matrix.
> Since octave does not automatically promote a vector to a matrix, I
> did this:
>
> a =
>
>   0.095555
>   0.789421
>   0.122409
>
> octave:12> b=diag(a)*ones(3,4)
> b =
>
>   0.095555  0.095555  0.095555  0.095555
>   0.789421  0.789421  0.789421  0.789421
>   0.122409  0.122409  0.122409  0.122409
>
> I could then add b to the original matrix.
>
> Of course, the multiplication is inefficient.  I seem to remember
> there was a more direct way to do this transformation, but I can't
> find it in the docs.  Is there?

Sure
b=a*ones(1,4);

you will see the efficiency once you go to bigger
problems:
octave:7> a=rand(1000,1);tic;b=a*ones(1,1000);toc
ans = 0.22280
octave:8> a=rand(1000,1);tic;c=diag(a)*ones(1000,1000);toc
ans = 142.55
octave:9> 142.55/0.22280
ans = 639.81

A speedup of a factor of 639 is OK or?

--
Mit freundlichen Gruessen
Daniel Heiserer
-----
--------------------------------------------------------------
Dipl.-Phys. Daniel Heiserer, BMW AG, Knorrstrasse 147, 80788 Muenchen
Abteilung EK-20
Tel.: 089-382-21187, Fax.: 089-382-42820

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