Efficient way in MATLAB to apply the same left and right matrix multiplication to a large set of matrices
By : rahul lodhi
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Your biggest problem is not the loops. For matrices so small calling MATLABs A*B introduces a lot of overhead. The best thing you can do is to store all the matrices in a large 4 x n_matrices matrix and spell out the matrix multiplications manually: code :
A = rand(4, 1000);
B = rand(4, 1000);
tic;
C = zeros(size(A));
C(1,:) = A(1,:).*B(1,:) + A(3,:).*B(2,:);
C(2,:) = A(2,:).*B(1,:) + A(4,:).*B(2,:);
C(3,:) = A(1,:).*B(3,:) + A(3,:).*B(4,:);
C(4,:) = A(2,:).*B(3,:) + A(4,:).*B(4,:);
toc
Elapsed time is 0.020950 seconds.

Do inner workings of Matlab do Full Matrix Multiplication when Matrix Multiplication is Argument for Trace?
By : Dima Koshelev
Date : March 29 2020, 07:55 AM
this will help Yes, MATLAB calculates the product, but you can avoid it! First, let's see what MATLAB does if you do f = trace(A*B): code :
tic
n = 6e3;
A = rand(n);
B = rand(n);
f = trace(A*B);
toc
pause(10)
tic
C(n) = 0;
for ii = 1:n
C(ii) = sum(A(ii,:)*B(:,ii));
end
g = sum(C);
toc
abs(fg) < 1e10
Elapsed time is 11.982804 seconds.
Elapsed time is 0.540285 seconds.
ans =
1
x = A\b;
x = inv(A)*b;

Matlab efficient sparse matrix multiplication
By : Ryuzaki dono
Date : March 29 2020, 07:55 AM
With these it helps I have a sparse matrix which only has elements in three diagonals. E.g. , Here's one way: code :
C = bsxfun(@times, A, B)

Efficient matrix multiplication in Matlab
By : Ishant Kadu
Date : March 29 2020, 07:55 AM
Hope this helps Take advantage of the implicit expansion feature of bsxfun. Use permute to have your B as an Nx1xM matrix:

Efficient multiplication of a matrix and a "list of matrices" in MATLAB
By : Sheikh
Date : March 29 2020, 07:55 AM
this one helps. TL;DR: this turned out to be slower than your code, I just wanted to share what I tried anyway...

