help-octave
[Top][All Lists]

## Efficient multiplication by a diagonal matrix

 From: Mario Storti Subject: Efficient multiplication by a diagonal matrix Date: Tue, 12 Nov 1996 19:21:19 -0300

```I found  myself repeatedly with the following  problem. Given a matrix
A(n,m)  and a vector  v(n), I  have to  multiply   each row  A(j,:) by
v(j). This is equivalent to compute:

B = diag(v) * A                     (1)

Now, for large    n, (1) is  very  inefficient,   because it  requires
constructing the square matrix diag(v) which requires storage and many
redundant operations since most elements  of diag(v) are null. If n>>m
then:

B= kron(v,ones(1,m)).*A             (2)

does the job  and is better.  But the more  efficient way is computing
row by row if m>>n and column by column  if n>>m. However, I repeat, I
find this problem so many times and in so many  areas that it seems to
me that some system call should do it.

I wrote some code  of my own  to  do this  task,  but I wonder  if I'm
redeveloping the wheel. Does anyone have a betetr solution?

Mario

%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%
Mario Alberto Storti               | Fax: (54)(42) 55.09.44               |
Grupo de Tecnologia Mecanica       | Tel: (54)(42) 55.91.75               |
INTEC, Guemes 3450 - 3000 Santa Fe | http://venus.unl.edu.ar/gtm-eng.html |
Argentina                          | Home: Gob. Vera 3161                 |