How to reduce running time of diagonal matrix multiplication with full matrix in Matlab?

44 次查看(过去 30 天)
I need to calculate a matrix multiplication that , where B is a full matrix with and D is a digonal matrix with .The computational complexity is
Acturally, if the matrix D is a full matrix, the computational complexity will be .
I recorded the running time for both cases in matlab, and find that the running time and time complexity are not consistent, how can I speed it up? I want to use less time to calculate the first case compared with the second case.
Thanks.
  2 个评论
Chunru
Chunru 2022-7-28
The simplest case is for D=I. Then . The complexity for this matrix multiplication is rather than .
For full matrix D, the complexity is .
For diagonal D, the complexity is .

请先登录,再进行评论。

采纳的回答

Chunru
Chunru 2022-7-28
编辑:Chunru 2022-7-28
n = 2000; d=500;
B = randn(d, n);
dv = randn(n, 1);
D = diag(dv);
% Normal
tic
A = B*D*B';
toc
Elapsed time is 0.052677 seconds.
% Speed up 1
tic
C = B*(dv.*B');
toc
Elapsed time is 0.012931 seconds.
% Speed up 2
tic
E = (B.*dv')*B';
toc
Elapsed time is 0.012015 seconds.
A(1:5, 1:5)
ans = 5×5
6.0809 11.2463 -58.6430 -66.3057 -24.0124 11.2463 -49.0177 52.5213 23.6997 18.0264 -58.6430 52.5213 42.9952 29.0237 23.4184 -66.3057 23.6997 29.0237 -44.6983 -0.1757 -24.0124 18.0264 23.4184 -0.1757 -61.5623
C(1:5, 1:5)
ans = 5×5
6.0809 11.2463 -58.6430 -66.3057 -24.0124 11.2463 -49.0177 52.5213 23.6997 18.0264 -58.6430 52.5213 42.9952 29.0237 23.4184 -66.3057 23.6997 29.0237 -44.6983 -0.1757 -24.0124 18.0264 23.4184 -0.1757 -61.5623
E(1:5, 1:5)
ans = 5×5
6.0809 11.2463 -58.6430 -66.3057 -24.0124 11.2463 -49.0177 52.5213 23.6997 18.0264 -58.6430 52.5213 42.9952 29.0237 23.4184 -66.3057 23.6997 29.0237 -44.6983 -0.1757 -24.0124 18.0264 23.4184 -0.1757 -61.5623

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by