MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   MATLAB中超大型矩阵的有效乘法 (https://www.labfans.com/bbs/showthread.php?t=24217)

poster 2019-12-10 20:48

MATLAB中超大型矩阵的有效乘法
 
由于D很大,我没有足够的内存来简单地创建对角D-D矩阵。我不断收到“内存不足”错误。

我没有执行M x D运算,而不是在第一次乘法中执行M x D x D运算,但是我的代码仍然需要花一些时间才能运行。

有人能找到一种更有效的方法来执行乘法A'*B*A吗?到目前为止,这是我尝试过的事情:

D=20000 M=25 A = floor(rand(D,M)*10); B = floor(rand(1,D)*10); for i=1:D for j=1:M result(i,j) = A(i,j) * B(1,j); end end manual = result * A'; auto = A*diag(B)*A'; isequal(manual,auto) [IMG]https://i.stack.imgur.com/054bl.jpg[/IMG]



[B]回答:[/B]

解决[URL="http://www.mathworks.com/help/techdoc/ref/sparse.html"]稀疏矩阵的[/URL]一种方法是使用[URL="http://www.mathworks.com/help/techdoc/ref/sparse.html"]稀疏矩阵[/URL] 。这是一个例子:

D = 20000; M = 25; A = floor(rand(D,M).*10); %# A D-by-M matrix diagB = rand(1,D).*10; %# Main diagonal of B B = sparse(1:D,1:D,diagB); %# A sparse D-by-D diagonal matrix result = (A.'*B)*A; %'# An M-by-M result 另一种选择是使用函数[URL="http://www.mathworks.com/help/techdoc/ref/repmat.html"]REPMAT[/URL]沿B的主对角线复制D元素以创建M by D矩阵,然后与A.' [URL="http://www.mathworks.com/help/techdoc/ref/repmat.html"]逐[/URL] [URL="http://www.mathworks.com/help/techdoc/ref/arithmeticoperators.html"]元素乘法[/URL] A.' :

B = repmat(diagB,M,1); %# Replicate diagB to create an M-by-D matrix result = (A.'.*B)*A; %'# An M-by-M result 还有另一个选择是使用功能[URL="http://www.mathworks.com/help/techdoc/ref/bsxfun.html"]BSXFUN[/URL] :

result = bsxfun(@times,A.',diagB)*A; %'# An M-by-M result

[url=https://stackoverflow.com/questions/4420235]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 03:03

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.