comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: multiplication by a diagonal matrix
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: multiplication by a diagonal matrix [message #40806] Fri, 03 September 2004 11:03 Go to next message
gnarloo is currently offline  gnarloo
Messages: 14
Registered: June 2004
Junior Member
guarda,
if you build a matrix with the n vectors (the weights)
and then you write

w*a
idl performs the multiplication element by element
lascia stare diag_matrix che complica solo le cose, idl supporta
le stesse notationi tra matrici e numeri e se scrivi w*a
ti fa direttamente il conto che vuoi tu.

but first you have to build the matrix out of the set of weight vectors

ciao

> I have the following problem: given a matrix A(n,m) and a vector of
> weighting factors w(n), i need to multiply each row of the matrix
> A(i,*)by the corresponding weighting factor w(i).
>
> I know that I can simply "transform" the w vector into a diagonal
> matrix with diag_matrix and then multiply it with A (e.g.: result =
> A##diag_matrix(w)), but for large values of n this solution is very
> slow.
>
> Can anybody suggest me a faster approach to solve this problem ?
>
> Thanks in advance for the help,
>
> Lorenzo Busetto
>
> Remote Sensing Lab.
> University of Milano-Bicocca.
Re: multiplication by a diagonal matrix [message #40807 is a reply to message #40806] Fri, 03 September 2004 08:01 Go to previous messageGo to next message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Fri, 03 Sep 2004 03:53:06 -0700, Lorenzo Busetto wrote:

> Hi all,
>
> I have the following problem: given a matrix A(n,m) and a vector of
> weighting factors w(n), i need to multiply each row of the matrix A(i,*)by
> the corresponding weighting factor w(i).
>
> I know that I can simply "transform" the w vector into a diagonal matrix
> with diag_matrix and then multiply it with A (e.g.: result =
> A##diag_matrix(w)), but for large values of n this solution is very slow.
>
> Can anybody suggest me a faster approach to solve this problem ?

I have had luck with the SPRSIN and SPRSAB, the numerical recipes sparse
matrix routines IDL includes.

JD
Re: multiplication by a diagonal matrix [message #40809 is a reply to message #40807] Fri, 03 September 2004 07:42 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
lbusett@yahoo.it (Lorenzo Busetto) writes:


> Hi all,
>
> I have the following problem: given a matrix A(n,m) and a vector of
> weighting factors w(n), i need to multiply each row of the matrix
> A(i,*)by the corresponding weighting factor w(i).

It sounds like you want to use a FOR loop. When N is small, the
number of loop iterations is small, so it will be fast. When N is
large, the number of loop iterations is larger too, but you also get
more done per iteration (N multiplies per iteration), plus you save
N*(N-1) elements of memory compared to the full matrix approach.

Craig

Good luck,
Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: multiplication by a diagonal matrix [message #40810 is a reply to message #40809] Fri, 03 September 2004 05:32 Go to previous messageGo to next message
Paolo Grigis is currently offline  Paolo Grigis
Messages: 171
Registered: December 2003
Senior Member
Lorenzo Busetto wrote:
> Hi all,
>
> I have the following problem: given a matrix A(n,m) and a vector of
> weighting factors w(n), i need to multiply each row of the matrix
> A(i,*)by the corresponding weighting factor w(i).
>
> I know that I can simply "transform" the w vector into a diagonal
> matrix with diag_matrix and then multiply it with A (e.g.: result =
> A##diag_matrix(w)), but for large values of n this solution is very
> slow.
>
> Can anybody suggest me a faster approach to solve this problem ?
>
> Thanks in advance for the help,
>
> Lorenzo Busetto
>
> Remote Sensing Lab.
> University of Milano-Bicocca.

You could try (if you *really* want to avoid a FOR loop over the rows):

N=n_elements(w)
ahelp=replicate(1d,N)##w
res=a*ahelp

In the second line, * is much faster than ##, and the first
line is just a 1 by N matrix multiplication, faster than the
N by N you used.


Ciao,
Paolo
Re: multiplication by a diagonal matrix [message #40928 is a reply to message #40806] Wed, 08 September 2004 05:50 Go to previous message
lbusett is currently offline  lbusett
Messages: 9
Registered: March 2004
Junior Member
Hi all,

thanks for your answers !

I made some tests with different methods and for a matrix
A(10000,10)I had the following results:


"diag_matrix" method : processing time = 1600 seconds (!!)
"for loop" method: processing time = 0.02 seconds
"replicate" method (as suggested by Paolo Grigis in his answer):
processing time = 0.002 seconds

Well, when I posted my message I'd never expected to increase the
speed 800000 times ! Can it be possible ?

Thanks for the help.

Lorenzo Busetto

Remote Sensing Lab.
University of Milano-Bicocca
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: event_pro for compound widgets
Next Topic: multiplication by a diagonal matrix

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 07:01:45 PDT 2025

Total time taken to generate the page: 0.24494 seconds