Re: multiplication by a diagonal matrix [message #40806] |
Fri, 03 September 2004 11:03  |
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 #40809 is a reply to message #40807] |
Fri, 03 September 2004 07:42   |
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   |
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  |
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
|
|
|