필터 지우기
필터 지우기

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

조회 수: 3 (최근 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 .
z cy
z cy 2022년 7월 28일
Thanks, I made a typo. I have modified it in the question

댓글을 달려면 로그인하십시오.

채택된 답변

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