1
votes

I'm having some trouble vectorizing a part of my code. I have a (n,n,m) tensor, and I want to multiply each slice in m by a second (n by n) matrix (NOT element wise).

Here's what it looks like as a for-loop:

Tensor=zeros(2,2,3);
Matrix = [1,2; 3,4];

for j=1:n
    Matrices_Multiplied = Tensor(:,:,j)*Matrix;
    Recursive_Matrix=Recursive_Matrix + Tensor(:,:,j)/trace(Matrices_Multiplied);
end

How do I perform matrix multiplication for individual matrices inside a tensor in a vectorized manner? Is there a built-in function like tensor-dot that can handle this or is it more clever?

1

1 Answers

1
votes

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.