C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# How to do the following matrix multiplication more efficient in Matlab?

By : ThreeSecSnare
Date : November 22 2020, 09:00 AM
hope this fix your issue You can greatly reduce the number of operations using associativity and the transpose-product property:
code :
``````t = B*A*b;
a = abs(t).^2;
``````

Share :

## 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(f-g) < 1e-10

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)
``````