How to multiply two matrices with different dimensions with high speed

Imagine that I have two matrices like this:
A = (1×51)
B = (30×50)
If I want to multiply these two matrices directly in matlab. I mean, for example A×B, must be in the form of 51×30×50 . You may tell me to do this :
C = A.*B(:);
D = reshape(C,[51 30 50]);
But I am in the condition that I'd rather increase speed by deleting vectorization operations (like ":"). That's why I'd like to get this matrix directly.

4 Comments

That is not slow by any means -
A = rand(1,51);
B = rand(30,50);
tic
for k=1:5e5
C = A.*B(:);
D = reshape(C,[51 30 50]);
end
toc
Elapsed time is 5.945659 seconds.
Less than 6 seconds for 50k iterations.
Here's a slightly faster way, which is basically the same operation using a different syntax -
%Using reshape instead of (:)
tic
for k=1:5e5
E = A.*reshape(B, [], 1);
F = reshape(E,[51 30 50]);
end
toc
Elapsed time is 5.965052 seconds.
And what makes you think that using (:) is a bottleneck?
I'd rather increase speed by deleting vectorization operations (like ":")
Why do you think that could increase speed?

If I could perform this manipulation directly, I would not need to perform ":" and "reshape" and I would get a "51×30×50" array directly. Apparently, this is not possible in MATLAB.

"If I could perform this manipulation directly, I would not need to perform ":" and "reshape" and I would get a "51×30×50" array directly. Apparently, this is not possible in MATLAB."
Please find the mathematics for directly doing that. I will be more than happy to write a code for you then.
If you are going to ignore the queries me and Matt raised and the answer Walter gave, atleast make a convincing point with some concrete evidence and information.

Sign in to comment.

Answers (1)

The (:) operator is one of the very fastest operators in MATLAB. It leaves the data pointer completely intact and just rewrites the header
'B' not-complex not-global 1 60002c491f20 2 30 50
(name) (flags) (count) (data pointer) (ndims) (dims)
becomes
'' not-complex not-global 2 60002c491f20 2 1500 1
with no rewriting of the data area (exact logic about the handling of the reference count may not be correct.)

3 Comments

Is there a difference between matrix(:) and reshape(matrix, [], 1) ?
As I understand it, you should prefer reshape over (:).', but I don't know if there is a difference without the transpose.
reshape() is defined as only affecting the header.
(:) is not defined that way. (:) is defined in terms of subsref() . In practice. (:) is optimized .
But I seem to recall that (:) for complex values historically triggered a copy whereas reshape() did not. But when I tested it in R2023b using format debug the data pointers were the same... so that is a behaviour that might have changed over time.

Sign in to comment.

Categories

Products

Release

R2018b

Asked:

on 22 Dec 2023

Commented:

on 23 Dec 2023

Community Treasure Hunt

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

Start Hunting!