フィルターのクリア

Column multiplication of two 2D matrices.

1 回表示 (過去 30 日間)
Nikan Fakhari
Nikan Fakhari 2021 年 1 月 13 日
編集済み: Bruno Luong 2021 年 1 月 14 日
Hi there,
I have two quite large matrices with the following sizes:
A = rand(94037,2240) B=rand(2240,2240)
I would like to mutiply each column of A by the transpose of each column of B (bascially two vector multiplication). Therefore each mutiplcation gives me a 2D matrix wih size (94037,2240) and I would like to store the result of each multiplication in a 3d MATRIX.
The thing is I can do this with for-loop as show below but its very slow and in the middle of operation matlab exist, because its quite heavy.
S = zeros(94037,2240,2240);
for i=1:1:2240
S(:,:,i) =A(:,i)*(B(:,i))';
end
Is there anyway I can do this without using for loop to speed up the process ?
Thanks for the help.
Best,
Nikan
  2 件のコメント
Matt J
Matt J 2021 年 1 月 13 日
Outer products are often both an inefficient and an avoidable step in many computations. What do you plan to do with S once you've computed it?
Nikan Fakhari
Nikan Fakhari 2021 年 1 月 13 日
I am planning to take sum along the third dimension

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

回答 (2 件)

Matt J
Matt J 2021 年 1 月 13 日
編集済み: Matt J 2021 年 1 月 13 日
I am planning to take sum along the third dimension
That would simply give you A*B.'

Matt J
Matt J 2021 年 1 月 13 日
編集済み: Matt J 2021 年 1 月 14 日
I doubt you will be able to do the computation with the matrix sizes you've indicated. The result will consume 3.5 TB of RAM in double floating point. However, if you can scale down the matrix sizes, then a loop-free solution is as follows,
[ma,na]=size(A);
[mb,nb]=size(B);
S=reshape(A,ma,1,na).*reshape(B,1,mb,nb);
  2 件のコメント
Bruno Luong
Bruno Luong 2021 年 1 月 14 日
編集済み: Bruno Luong 2021 年 1 月 14 日
I guess there is a typo. The right command has no brakets
S = reshape(A,ma,1,na) .* reshape(B,1,mb,nb);
Matt J
Matt J 2021 年 1 月 14 日
Right!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by