필터 지우기
필터 지우기

How to avoid repeated computation when the result is a symmetric matrix?

조회 수: 2 (최근 30일)
Xh Du
Xh Du 2017년 3월 29일
답변: Xh Du 2017년 3월 31일
Hi all,
When obtaining a symmetric matrix, we know that we only need to compute and store the elements of upper triangular part. Is there a way to only perform these computations related to the upper triangular part, such that the total number of computation can be reduced by almost half?
Check this example:
clear; clc;
a = rand(5, 1);
b = rand(5, 1);
c = rand(5, 1);
x = {a b c};
xtx = zeros(3, 3);
for i = 1:3
for j = 1:3
pass = x{i}' * x{j};
xtx(i, j) = xtx(i, j) + pass;
end
end
'xtx' is symmetric, but the for loop here computed all elements. Total number of vector products is 9, while we know only 6 products are really needed. If the number of computations is large, then the saving can be dramatic. So how can we solve this?
Many thanks!
  댓글 수: 4
Xh Du
Xh Du 2017년 3월 30일
so for i = j, the inner loop should be:
for j = i:3
Adam
Adam 2017년 3월 30일
I started out with that and changed it. You can just calculate the diagonal separately, but I guess j = i:3 is fine after all.

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

채택된 답변

Xh Du
Xh Du 2017년 3월 31일
Guess answer is in the comment, credit: Adam.
https://uk.mathworks.com/matlabcentral/answers/332568-how-to-avoid-repeated-computation-when-the-result-is-a-symmetric-matrix#comment_441260

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by