Hi all,
In my project I have to calculate the trace of some matrix products, I have the following script to demonstrate the purpose:
clear; clc;% number of total tests.
nTest = 500;% part 1. generate nTest*2 random matrices.
nd = 1000;nt = 100;dis = cell(nTest, 2);dis = cellfun(@(v) rand(nd, nt), dis, 'un', 0);% part 2. perform truncated-SVD on each matrix,
% only leave nRem singular vectors and values.
nRem = 50;disSVD = cell(nTest, 2);for isvd = 1:nTest for jsvd = 1:2 [u, s, v] = svd(dis{isvd, jsvd}, 0); disSVD{isvd, jsvd} = {u(:, 1:nRem), s(1:nRem, 1:nRem), v(:, 1:nRem)}; endend% part 3. for each SVD result, perform trace to obtain disTrans. disTrans is
% non-symmetric, thus jtr needs to start from 1.
disTrans = zeros(nTest);for itr = 1:nTest u1 = disSVD{itr, 1}; for jtr = 1:nTest u2 = disSVD{jtr, 2}; disTrans(itr, jtr) = ... trace(u1{3} * u1{2}' * u1{1}' * u2{1} * u2{2} * u2{3}'); endend
I ran profile to find out which part is the slowest, it turns out it's calculating the trace in part 3 due to the large number. Unfortunately in my project the number of calculating trace is also very large. So any idea of how to improve the speed of calculating the trace? The profile is shown here:
Many thanks!
Best Answer