Fast matrix multiplication with diagonal matrices
Mostrar comentarios más antiguos
Let Wbe a large, sparse matrix. Let
and
be diagonal matrices of the same size. I would like to calculate
. However, these matrices are large enough that matrix multiplication is very expensive. I would like to speed up the calculation of L.
I know that computing L can be sped up by utilizing the fact that
and
are diagonal. For example, I know that I can compute
as follows.
diagD1 = diag(D1); % diagonal of the matrix D1.
D1W = W.*diadD1; % Equivalent to multiplying the ith row of W by D(i,i). Yields D1*W.
My question is whether there is a similar exploitation of the diagonality of
that will allow me to avoid matrix multiplication to compute L.
Thank you.
6 comentarios
David Goodmanson
el 24 de Feb. de 2021
Hi Samuel,
are you sure about this? diagD1 is a column vector. So D1W = W*diagD1 is a column vector, not a matrix.
Samuel L. Polk
el 24 de Feb. de 2021
Editada: Samuel L. Polk
el 25 de Feb. de 2021
David Goodmanson
el 24 de Feb. de 2021
Editada: David Goodmanson
el 24 de Feb. de 2021
Yes, I was mistaken. However, the transpose gets it done.
w = rand(5,5)
d = diag(rand(5,5))
A = w.*d
B = w.*d'
A./w % each row is multiplied by the same element of d
B./w % each col is multiplied by the same element of d
Samuel L. Polk
el 25 de Feb. de 2021
It doesn't appear to be beneficial to avoid matrix multiplication, though I am surprised the latter is so slow (in R2020b).
N=1e5;
W=sprand(N,N,100/N);
d=rand(N,1);
D=spdiags(d,0,N,N);
tic
L1=D*W*D;
toc
tic;
L2=(d.*W).*d.';
toc
saskia leary
el 20 de Mzo. de 2022
(diag(D))'.*A works for right mulipltication - i.e.
A*D=(diag(D)).*A
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!