Bsxfunning
and using efficient matrix-multiplication
, we could do -
% Calculate trace values using matrix-multiplication
T = reshape(Matrix.',1,[])*reshape(Tensor,[],size(Tensor,3));
% Use broadcasting to perform elementwise division across all slices
out = sum(bsxfun(@rdivide,Tensor,reshape(T,1,1,[])),3);
Again, one can replace the last step with one more matrix-multiplication for possible further boost in performance. Thus, an all matrix-multiplication dedicated solution would be -
[m,n,r] = size(Tensor);
out = reshape(reshape(Tensor,[],size(Tensor,3))*(1./T.'),m,n)
Runtime test
Benchmarking code -
% Input arrays
n = 100; m = 100;
Tensor=rand(n,n,m);
Matrix=rand(n,n);
num_iter = 100; % Number of iterations to be run for
tic
disp('------------ Loopy woopy doops : ')
for iter = 1:num_iter
Recursive_Matrix = zeros(n,n);
for j=1:n
Matrices_Multiplied = Tensor(:,:,j)*Matrix;
Recursive_Matrix=Recursive_Matrix+Tensor(:,:,j)/trace(Matrices_Multiplied);
end
end
toc, clear iter Recursive_Matrix Matrices_Multiplied
tic
disp('------------- Bsxfun matrix-mul not so dull : ')
for iter = 1:num_iter
T = reshape(Matrix.',1,[])*reshape(Tensor,[],size(Tensor,3));
out = sum(bsxfun(@rdivide,Tensor,reshape(T,1,1,[])),3);
end
toc, clear T out
tic
disp('-------------- All matrix-mul having a ball : ')
for iter = 1:num_iter
T = reshape(Matrix.',1,[])*reshape(Tensor,[],size(Tensor,3));
[m,n,r] = size(Tensor);
out = reshape(reshape(Tensor,[],size(Tensor,3))*(1./T.'),m,n);
end
toc
Timings -
------------ Loopy woopy doops :
Elapsed time is 3.339464 seconds.
------------- Bsxfun matrix-mul not so dull :
Elapsed time is 1.354137 seconds.
-------------- All matrix-mul having a ball :
Elapsed time is 0.373712 seconds.