Hi,
The following computations are very expensive for large matrices A, B and the coefficient q.
C=reshape(A,a1*q,b1)*B;
C=reshape(C,a1,b2*q);
a1 and a2 are the dimensions of A, while b1 and b2 are the dimensions of B and with the restriction that a2=b1*q.
In my applications, matrices A and B can have dimensions in the orders of thousands or more.
There is one way of implementing these operations using loops
C=zeros(a1,b2*q);
acols=0:q:(b1-1)*q;
ccols=0:q:(b2-1)*q;
for ii=q:-1:1
C(:,ccols+ii)=A(:,acols+ii)*B;
end
but that strategy is not as fast as the one above. Is there any way to accelerate these operations?
try them for instance with the following
b1=1300;
b2=500;
q=350;
a1=300;
a2=b1*q;
B=rand(b1,b2);
A=rand(a1,a2);
Thanks

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 7 月 25 日

0 投票

4 件のコメント

Patrick Mboma
Patrick Mboma 2015 年 7 月 25 日
Dear Azzi,
Thanks for the link. I think the problem is more about the reshaping than with the actual matrix multiplication. I am more than happy to run multiplication at Matlab's speed.
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 7 月 25 日
You can test the reshaping speed
b1=1300;
b2=500;
q=350;
a1=300;
a2=b1*q;
B=rand(b1,b2);
A=rand(a1,a2);
tic
C1=reshape(A,a1*q,b1);
toc
tic
C=C1*B;
toc
The reshaping takes 0.075 seconds
Elapsed time is 0.075266 seconds.
Elapsed time is 3.929999 seconds.
Patrick Mboma
Patrick Mboma 2015 年 7 月 25 日
Looks like I may have overlooked something... Thanks for pointing that out.
James Tursa
James Tursa 2015 年 7 月 26 日
Are any of the matrices sparse? For full matrices, reshape is extremely fast since it returns a shared data copy. But for sparse matrices, reshape is expensive since it requires a deep data copy.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by