How to multiply each row of a matrix by different matrices, without for loops?
1 view (last 30 days)
Show older comments
Jia Quan Yong
on 29 Nov 2019
Answered: Raunak Gupta
on 4 Dec 2019
n = 3; m = 5; k = 10;
A = ones(n,m);
B = randn(m,k);
% I want something similar to A*B with size (n x k)
% Instead of A*B, I want each row of A multiplied with a different matrix B1,B2,B3...,Bn each of size (m x k)
% i.e. A(row,:)*B(:,:,row)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% create B1,B2,B3 ... Bn each of size (m x k)
for i=1:n
B(:,:,i) = randn(m,k);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% do what was described with a for loop
result = A(1,:)*B(:,:,1); %head case
for i=2:n
concatenayte = A(i,:)*B(:,:,i);
result = [result ; concatenayte]; %concatenated from size (1 x k) to (2 x k) to ... to finally (n x k)
end
result=result %print
% visually speaking, e.g.
%
% [B1]
% [ A ] x [B2] = [ C ]
% [B3]
% 3 x m m x k 3 x k
What I want to do is multiply each row of A of size (n x m), with different matrices, namely n-number of B's each of size (m x k).
I have done my best in making clear what I want to achieve explicitly.
How can I do the multiplication efficiently i.e. without for-loop? Is it even possible with what is offered by MatLab?
Do I perhaps need Parallel Computing Toolbox (which I don't have at the moment) or anything?
% For more context, I am trying to code backpropagtion of a flat fully connected layer with batch normalization(before activation).
0 Comments
Accepted Answer
Raunak Gupta
on 4 Dec 2019
Hi,
For the application of implementing backpropagation, the matrix multiplication in above way will be efficient with the use of parfor since there is no space overhead by using above. Also, I suggest using gpuArray if the following implementation is required for larger matrices as it will utilize any GPU capabilities that are present with system.
As for the code that is shared, one optimization can be done by pre-allocating the result array because every time its size is changing which may slow down the code.
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!