Speed up Matrix multiplication

6 ビュー (過去 30 日間)
Dustin Williams
Dustin Williams 2020 年 4 月 20 日
コメント済み: Dustin Williams 2020 年 4 月 20 日
I want to do the following Matrix Multiplication. Problem - Example:
A = rand(3,3,1000000);
B = rand(3,1000000)
How to calculate C faster than with this For-loop? I tried parfor but its only slightly faster.
Furthermore i'd prefer not to install the Parallel Computing Toolbox or MTIMESX. Is it possible just with reshape/permute/bsxfun?
C = zeros(3,1,size(B,2));
for idx=1:size(B,2)
C(:,:,idx) = A(:,:,idx) * B(:,idx);
end
Thanks!
  1 件のコメント
Michael Soskind
Michael Soskind 2020 年 4 月 20 日
I see a marginal performance improvement by simply getting rid of the middle array index:
A = rand(3,3,1e6);
B = rand(3,1e6);
% original code
tic
C = zeros(3,1,size(B,2));
for idx=1:size(B,2)
C(:,:,idx) = A(:,:,idx) * B(:,idx);
end
toc
% modified code with one fewer array dimension
tic
C = zeros(3,size(B,2));
for idx=1:size(B,2)
C(:,idx) = A(:,:,idx) * B(:,idx);
end
toc
Not sure if that helps or hurts you, but I get about 0.3 sec faster execution time, on average (Goes from 2 sec to 1.7 sec).
Michael

サインインしてコメントする。

採用された回答

Matt J
Matt J 2020 年 4 月 20 日
編集済み: Matt J 2020 年 4 月 20 日
C=sum( A.*reshape(B,1,3,[]),2);
  2 件のコメント
Matt J
Matt J 2020 年 4 月 20 日
Or, on pre-R2016b versions,
C=sum( bsxfun( @times, A, reshape(B,1,3,[]) ) ,2)
Dustin Williams
Dustin Williams 2020 年 4 月 20 日
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by