Fast matrix multiplication with diagonal matrices
显示 更早的评论
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 个评论
David Goodmanson
2021-2-24
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
2021-2-24
编辑:Samuel L. Polk
2021-2-25
David Goodmanson
2021-2-24
编辑:David Goodmanson
2021-2-24
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
2021-2-25
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
2022-3-20
(diag(D))'.*A works for right mulipltication - i.e.
A*D=(diag(D)).*A
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!