How to improve the speed of computing trace in my code?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Pieter Hamming
am 20 Jun. 2018
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.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!