Matrix Product optimization with Bsxfun

Dear community,
i am looking to do:
A % --> 128 x 128 (matrix)
B % --> 1 x 128 (vector)
And i need to calculate (repeated 1e5 times):
D = A*B'
% that is not the element wise A.*B
For speed, i have problem by doing the fast:
D = bsxfun(@mtimes, A, B');
That should be the same but gives me error.
How can i fix this regarding dimensions?

7 Commenti

A = rand(128);
B = rand(128,1);
D = A*B'
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix
individually, use TIMES (.*) for elementwise multiplication.
B = rand(1, 128);
It should work in this way. My problem was with bsxfun by the way.
Torsten
Torsten il 5 Nov 2022
Modificato: Torsten il 5 Nov 2022
How should
bsxfun(@mtimes,A,B')
(even if it worked) be faster than
A*B'
?
https://stackoverflow.com/questions/12951453/in-matlab-when-is-it-optimal-to-use-bsxfun
It doesn't apply to your case - it's a simple matrix-vector-product without any expansion or anything.
Nothing will be faster than
D = A*B.'
Note that this question on Stack Overflow was asked ten years ago. Much has changed in MATLAB in the past decade, including the introduction of implicit expansion in release R2016b (as one of the answers on that question mentions.)
If you're computing the matrix product of a 128-by-128 matrix and a 128-by-1 vector, you need neither bsxfun nor implicit expansion. Just use normal matrix multiplication. If that's not what you're trying to do, please explain in more detail why you're asking about bsxfun or implicit expansion.
OK, thanbk you Steven.

Accedi per commentare.

 Risposta accettata

Bruno Luong
Bruno Luong il 5 Nov 2022
Modificato: Bruno Luong il 5 Nov 2022
You simply cannot invent something that does not exist (support) in MATLAB, the function mtimes is not supported by bsxfun see fun — Binary function to apply
Beside that I have the hard time to figure out what you would ;do with matrix multiplication with expansion

14 Commenti

Ok, thank you. How can i improve my code? Do you have any suggestions?
It is not clear to me why you compute
D = A*B'
repeatly 1e5 time? There is nothing change 1e5 times so why you repeat the calculation that is invariant?
No it's in a computation where A and B changes. This is why i need speed.
Try this piece of code and you can see with profiler that is not computational efficient.
for i = 1:1e5
A = rand(512);
B = rand(1,512);
D = A*B';
fprintf("Results: \n");
fprintf("%g ", D);
fprintf("\n");
end
Then of course my code is different but i have a lot of time consumption in this part. Just to optimize this piece/section is my target.
Your common sense should tell you that if A and B arbitrarily change in this loop, there is no other way than to code it as
D = A*B.'
. The only thing that is computational inefficient are the fprintf commands.
Thank you Torsten
The profiler result attached (on my computer) shows the matrix vector multiplcation lasts 0.7 sec, 1/10 of the total time. 9 times faster than simple generating matrix with RANDN.
Without profiler it's 4 times fater, 0.17 sec for 1e5 matrix-multiplations in a for loop.
profile on
for i = 1:1e5
A = rand(128);
B = rand(1,128);
D = A*B';
end
profile off
profile viewer
tic
for i = 1:1e5
D = A*B';
end
toc % 0.178779
I can't see anyway to make it much faster (and the need for). Here you are fighting on what MATLAB doing best (matrix arithmetics)
By the way i got no speed in time:
clear; close all; clc
tic;
for i = 1:1e4
A = rand(512);
B = rand(1,512);
D = A*B';
end
toc;
tic;
for i = 1:1e4
A = rand(512);
B = rand(1,512);
D = A*B.';
end
toc;
Try with 1e4 that is similar and you get something like this:
Elapsed time is 19.338468 seconds.
Elapsed time is 19.678149 seconds.
Your original post mentions matrix of dimesion 128, but now you post dimension 512.
Please don't do it when asking question, it's count productive.
You are right bruno, the time consumption was related to RAND not to the multiplication .Thank you, it's faster, right.
Bruno Luong
Bruno Luong il 5 Nov 2022
Modificato: Bruno Luong il 5 Nov 2022
What you want to show with your code? Both tic/toc measure the identical piece of code to me, conjugate transpose on real matrix has no effect, so it is normal they run about the same time.
BTW I get avout 13.5 sec on my computer.
Oh ok yes i tried now to use more simulations and i get with 1e6:
Elapsed time is 68.102781 seconds.
Elapsed time is 69.234393 seconds.
So well, it's not clear to me if A*B' is faster than A*B.' but by the way thank you so much.
No in my original code A, B has real and imaginary parts. This was only for performance comparison.
Note that there is a big difference between ' and .' for complex data, and only you know which one you mean:
A=[1 1+i];
A' , A.'
ans =
1.0000 + 0.0000i 1.0000 - 1.0000i
ans =
1.0000 + 0.0000i 1.0000 + 1.0000i
The runtime should be about the same even for complex data between matrix vector multiplcation with ' and .', since it takes care by the Blas and there is no conjugate explicitly performed.

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2022b

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by