How to improve the speed of computing trace in my code?
조회 수: 2 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
답변 (1개)
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!