How to improve the speed of computing trace in my code?

조회 수: 2 (최근 30일)
Xiaohan Du
Xiaohan Du 2018년 6월 20일
댓글: Xiaohan Du 2018년 6월 20일
Hi all,
In my code there is a key function which cost the majority of computational power due to the large number of repetitive computations. Imagine I have 2 cell arrays 'respi' and 'respj' which contains results of SVD vectors:
>> respi
respi =
1×10 cell array
Columns 1 through 9
{3×1 cell} {3×1 cell} {3×1 cell} {3×1 cell} {3×1 cell} {3×1 cell} {3×1 cell} {3×1 cell} {3×1 cell}
Column 10
{3×1 cell}
>> respi{1}
ans =
3×1 cell array
{242×5 double}
{ 5×5 double}
{ 3×5 double}
I have a function which takes respi and respj, compute and store the trace as follows
function otpt = uTuPartDemo(respi, respj)
otpt = zeros(length(respi), length(respj));
for iTr = 1:length(respi)
u1 = respi{iTr}; % find the ith cell element.
for jTr = 1:length(respj)
u2 = respj{jTr}; % find the jth cell element
otpt(iTr, jTr) = ... % compute the trace.
trace((u2{3}' * u1{3}) * u1{2}' * (u1{1}' * u2{1}) * u2{2});
end
end
And I need to run this function repetitively for many times as follows:
for i = 1:n % very large number, say n = 100000
otpt = uTuPartDemo(respi, respj);
end
How can I improve the speed and efficiency of function uTuPartDemo? Many thanks!

답변 (1개)

Pieter Hamming
Pieter Hamming 2018년 6월 20일
Presumably the trace calculation takes most time. Since you're only interested in the diagonals anyway, why not use the approach explained here. It should reduce your computational strain.
A minor other issue: you don't need to define u1 and u2. Instead of
u1 = respi{iTr};
u1{3}
Just use
respi{iTr}{3}
It will save you a minor amount of memory.
  댓글 수: 1
Xiaohan Du
Xiaohan Du 2018년 6월 20일
Hi Pieter,
Before trying this approach, I'd like to know if I need to recover the matrix before SVD (i.e. computing A = u1{1} * u1{2} * u1{3}', B = u2{1} * u2{2} * u2{3}'), then compute sum(A.*B',2)?

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

카테고리

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