poster
2019-12-10, 20:48
由于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) https://i.stack.imgur.com/054bl.jpg
回答:
解决稀疏矩阵的 (http://www.mathworks.com/help/techdoc/ref/sparse.html)一种方法是使用稀疏矩阵 (http://www.mathworks.com/help/techdoc/ref/sparse.html) 。这是一个例子:
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 另一种选择是使用函数REPMAT (http://www.mathworks.com/help/techdoc/ref/repmat.html)沿B的主对角线复制D元素以创建M by D矩阵,然后与A.' 逐 (http://www.mathworks.com/help/techdoc/ref/repmat.html) 元素乘法 (http://www.mathworks.com/help/techdoc/ref/arithmeticoperators.html) A.' :
B = repmat(diagB,M,1); %# Replicate diagB to create an M-by-D matrix result = (A.'.*B)*A; %'# An M-by-M result 还有另一个选择是使用功能BSXFUN (http://www.mathworks.com/help/techdoc/ref/bsxfun.html) :
result = bsxfun(@times,A.',diagB)*A; %'# An M-by-M result
更多&回答... (https://stackoverflow.com/questions/4420235)
我没有执行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) https://i.stack.imgur.com/054bl.jpg
回答:
解决稀疏矩阵的 (http://www.mathworks.com/help/techdoc/ref/sparse.html)一种方法是使用稀疏矩阵 (http://www.mathworks.com/help/techdoc/ref/sparse.html) 。这是一个例子:
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 另一种选择是使用函数REPMAT (http://www.mathworks.com/help/techdoc/ref/repmat.html)沿B的主对角线复制D元素以创建M by D矩阵,然后与A.' 逐 (http://www.mathworks.com/help/techdoc/ref/repmat.html) 元素乘法 (http://www.mathworks.com/help/techdoc/ref/arithmeticoperators.html) A.' :
B = repmat(diagB,M,1); %# Replicate diagB to create an M-by-D matrix result = (A.'.*B)*A; %'# An M-by-M result 还有另一个选择是使用功能BSXFUN (http://www.mathworks.com/help/techdoc/ref/bsxfun.html) :
result = bsxfun(@times,A.',diagB)*A; %'# An M-by-M result
更多&回答... (https://stackoverflow.com/questions/4420235)